• 组合数据类型练习,英文词频统计实例上


      1. 字典实例:建立学生学号成绩字典,做增删改查遍历操作。
      2. 列表,元组,字典,集合的遍历。
        总结列表,元组,字典,集合的联系与区别。
      3. 英文词频统计实例
        1. 待分析字符串
        2. 分解提取单词
          1. 大小写 txt.lower()
          2. 分隔符'.,:;?!-_’
          3. 单词列表
        3. 单词计数字典

    # 实例:建立学生学号成绩字典,做增删改查遍历操作。
     1 student_name = ['小叶','小在','小林','小红','小明','小东','小花']
     2 student_g = [100,99,98,97,96,95,94]
     3 d = dict(zip(student_name,student_g))
     4 # d = {'小叶':100,'小林':99}
     5 # 增加
     6 d['小紫'] = 93
     7 # 删除
     8 d.pop('小花')
     9 # 修改
    10 d['小东'] = 60
    11 # 查询
    12 d.get('小叶','')


    # 列表、元组、字典、集合的遍历
    l = list('23132111123132')
    t = tuple('1233211232123')
    s = set(l)
    d = dict(zip([1,2,3,4],[4,3,2,1]))
    for i in l:
        print(i)
    for i in t:
        print(i)
    for i in s:
        print(i)
    for i in d:
        print(i)

    # 总结列表、元组、字典、集合的联系与区别
    # (1)列表是任意对象的序列。列表用方括号表示。
    # (2)将一组值打包到一个对象中,称为元组。元组用圆括号表示。元组和列表的大部分操作相同。但是,列表是不固定的,可以随时插入,删除;而元组一旦确认就不能够再更改。所以,系统为了列表的灵活性,就需要牺牲掉一些内存;而元组就更为紧凑。
    # (3)与列表和元组不同,集合是无序的,也不能通过索引进行访问。此外,集合中的元素不能重复。
    # (4)字典就是一个关联数组或散列表,其中包含通过关键字索引的对象。用大括号表示。与集合相比,通过关键字索引,所以比集合访问方便。字典是Python解释器中最完善的数据类型。


    # 实例:词频统计
     1 str = '''
     2 When you wake up in the morning
     3 When you haven't started to think
     4 There is a whole brand new day
     5 Open wide and waiting for you
     6 I know in life's sorrow
     7 you're on the verge of drowning
     8 May your tears flee with yesterday
     9 blow away with the wind
    10 When you wake up in the morning
    11 When you haven't started to think
    12 The world is out there calling
    13 open eyes to new beginning
    14 A newborn sun is shinning
    15 Chasing shadows from your mind
    16 Everything will be alive,
    17 under the sunshine's smile
    18 Come out from your corner
    19 No doubt in join us
    20 You can decide the future
    21 Devote your youthful power to this world
    22 Come together, hand in hand together
    23 I know you'll do
    24 We pray and believe
    25 that tomorrow will be better.
    26 No, I don't know what your name is
    27 But you're so familiar to me
    28 Cause we belong to one family
    29 You can hear my heart calling
    30 Life can be music,
    31 rainbows can be reached
    32 If you face yourself truly
    33 keep striving for your dream
    34 Come out from your corner
    35 No doubt in join us
    36 You can decide the future
    37 Devote your youthful power to this world
    38 Come together, hand in hand together
    39 I know you'll do
    40 We pray and believe
    41 that tomorrow will be better.
    42 When you wake up in the morning
    43 When you haven't started to think
    44 There is a whole brand new day
    45 Open wide and waiting for you
    46 I know in life's sorrow
    47 you're on the verge of drowning
    48 May your tears flee with yesterday
    49 Blow away with the wind
    50 Come out from your corner
    51 No doubt in join us
    52 You can decide the future
    53 Devote your youthful power to this world
    54 Come together, hand in hand together
    55 I know you'll do
    56 We pray and believe
    57 that tomorrow will be better.
    58 '''
    59 # 将,?.!变成空格
    60 str = str.replace(',',' ')
    61 str = str.replace('
    ','')
    62 str = str.replace('  ','')
    63 # 将所有大写转换为小写
    64 str = str.lower()
    65 # 把歌词切片
    66 words = str.split(' ')
    67 # 定义一个空字典
    68 di = {}
    69 # 单词排序
    70 words.sort()
    71 # 用循环,写入字典
    72 disc = set(words)
    73 for i in disc:
    74     di[i] = 0
    75 for i in words:
    76     di[i] = di[i]+1
    77 items = di.items()
    78 
    79 print(items)
  • 相关阅读:
    抽象工厂例子
    学习boost::asio一些小例子
    boost::asio学习(定时器)
    共享内存
    网络流程图
    粘包
    端游服务器群
    38 写一个函数,求一个字符串的长度,在main函数中输入字符串,并输出其长度。
    37 有n个人围成一圈,顺序排号,从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来第几号那位.
    36 有n个整数,使其前面各数顺序向后移n个位置,最后m个数变成最前面的m个数
  • 原文地址:https://www.cnblogs.com/alliancehacker/p/7573298.html
Copyright © 2020-2023  润新知