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

时间:2023-02-13 20:27:12
  1. 字典实例:建立学生学号成绩字典,做增删改查遍历操作。
    1. >>> d={'liming':'100','zhangsan':'95','xiaohua':'80'}
      >>> d['xiaohua']
      '80'
      >>> d.pop('zhangsan')
      '95'
      >>> d
      {'liming': '100', 'xiaohua': '80'}
      >>> d['liming']=95
      >>> d
      {'liming': 95, 'xiaohua': '80'}
      >>> d['xiaohong']=80
      >>> d
      {'liming': 95, 'xiaohua': '80', 'xiaohong': 80}
      >>> for i in d:
      print(i,d[i])

      liming 95
      xiaohua 80
      xiaohong 80

       

       

  2. 列表,元组,字典,集合的遍历。
  3. a=list('1231')
    b=tuple('abcd')
    print(a)
    print(b)
    s=set(a)
    print(s)
    d=dict(zip(a,b))
    print(d)

    for i in a:
    print(i)
    for i in b:
    print(i)
    for i in s:
    print(s)
    for i in d:
    print(i,d[i])

    总结列表,元组,字典,集合的联系与区别:

    列表是一组值,其中的值可以改变
    元组也是一组值,其中的值不能改变
    列表与元组可以相互转换
    集合是一组唯一的无顺序的值
    字典是无固定顺序的键值对

3、英文词频统计实例

  1. 待分析字符串
  2. 2、分解提取单词
    1. 大小写 txt.lower()
    2. 分隔符'.,:;?!-_’
    3. 单词列表
  3. 单词计数字典
  4. a='''Panda is favored by people, because they are so lovely. Many people come to Sichuan to see the pandas in the zoo, but the media always report some bad behaviors. Some tourists throw away the rubbish and some adults let their kids stand in the handrail, just to get closer to pandas. We need to act politely, for protecting the environment and animals.'''
    a=a.lower()
    for i in '.,':
    a=a.replace(i,' ')
    words=a.split(' ')
    dt={}
    for i in words:
    dt[i]=a.count(i)

    for i in dt:
    print("{0:<10}{1}".format(i,dt[i]))

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