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

本文通过实例演示了如何使用Python中的字典、列表等数据结构进行学生的成绩管理,并进一步介绍了如何利用这些数据结构来统计英文文本中各单词出现的频率。
  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]))

转载于:https://www.cnblogs.com/JUNJUNER/p/7573336.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值