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


    1. 字典实例:建立学生学号成绩字典,做增删改查遍历操作。
      #创建字典
      d={'01':'90','02':'95','03':'89','04':'92','05':'88'}
      print(d)
      
      #增加
      d['06']=97
      print('增加一个学号为06的学生信息:',d)
      
      #查找
      print('查找出学号05的学生成绩:',d['05'])
      
      #删除
      d.pop('04')
      print('删除学号为04的学生信息:',d)
      
      #修改
      d['01']=93
      print('修改学号为01的学生成绩为93:',d)
      
      #遍历
      print('遍历:')
      for i in d:
          print(i,d[i])

    2. 列表,元组,字典,集合的遍历。
      总结列表,元组,字典,集合的联系与区别。
      #遍历列表
      l=['11','21','31']
      print('列表遍历:')
      for i in l:
          print(i)
      
      #遍历元组
      t=('41','51','61')
      print('元组遍历:')
      for i in t:
          print(i)
      
      #遍历字典
      d={'1':'71','2':'81','3':'91'}
      print('字典遍历:')
      for i in d:
          print(i,d[i])
      
      #遍历集合
      s=set([12,22,32])
      print('集合遍历:')
      for i in s:
          print(i)

    列表,元组,字典,集合的联系与区别:列表,元组是有顺序的,而字典和集合是没顺序的。列表是以[ ]形式表示,元组是以( )表示,字典以{ }表示,集合则是以[()]的形式表示。列表是可变对象,可以有增删改操作,而元组是只读的,不能修改。字典使用键-值(key-value)存储,键是不可变的对象。插入和查找速度快,不会随着键的增加而变慢,需要占用大量的内存。字典是用空间换取时间的一种方法。集合是一个无序不重复元素集, 基本功能包括关系测试和消除重复元素。

    3. 英文词频统计实例

    1. 待分析字符串
    2. 分解提取单词
      1. 大小写 txt.lower()
      2. 分隔符'.,:;?!-_’
      3. 单词列表
    3. 单词计数字典
      s='''Twinkle, twinkle, little star,
      How I wonder what you are. 
      Up above the world so high, 
      Like a diamond in the sky. 
      When the blazing sun is gone, 
      When he nothing shines upon, 
      Then you show your little light, 
      Twinkle, twinkle, all the night. 
      Then the traveller in the dark, 
      Thanks you for your tiny spark, 
      He could not see which way to go, 
      If you did not twinkle so. 
      In the dark blue sky you keep, 
      And often through my curtains peep, 
      For you never shut your eye, 
      Till the sun is in the sky. 
      As your bright and tiny spark, 
      Lights the traveller in the dark. 
      Though I know not what you are, 
      Twinkle, twinkle, little star. '''
      #全部转为小写
      s=s.lower()
      print('全部转为小写:')
      print(s)
      
      #分隔符'.,:;?!-_’
      for i in ',.?!:':
          s=s.replace(i,' ')
      print('分隔符替换为空格:')
      print(s)
      
      #分隔出一个个单词
      words=s.split(' ')
      print('分隔结果为:')
      print(words)
      
      #创建一个字典
      d={}
      
      #单词计数字典列表
      for i in words:
          d[i]=words.count(i)
      d=list(d.items())
      print('单词计数字典:')
      print(d)

  • 相关阅读:
    关于高等数学的学习笔记(一)(未完)
    第十七章 波粒二象性笔记
    网课摸鱼一时爽,提交笔记火葬场——网课物理笔记
    讲丶数学
    啥是快速幂
    2019年十一月(CSP S游记及AFO)
    luogu P3959(2017noipTG D2T2
    圆锥曲线:椭圆大题解题报告
    圆锥曲线:椭圆小题解题报告
    sql查询json格式中的内容
  • 原文地址:https://www.cnblogs.com/qisq/p/7595956.html
Copyright © 2020-2023  润新知