zoukankan      html  css  js  c++  java
  • 组合数据类型练习,英文词频统计实例上

    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)

  • 相关阅读:
    使用def文件简化dll导出
    ASP.NET Core MVC 之过滤器(Filter)
    ASP.NET Core 中间件 中间件(Middleware)和过滤器(Filter)的区别
    drf-apiview解读系列二
    干货分享,40个photoshop技能送给你!
    冒泡排序 深度优化
    数据结构与算法_14 _ 排序优化:如何实现一个通用的、高性能的排序函数
    数据结构与算法_12 _ 排序(下):如何用快排思想在O(n)内查找第K大元素
    数据结构与算法_13 _ 线性排序:如何根据年龄给100万用户数据排序
    数据结构与算法_11 _ 排序(上):为什么插入排序比冒泡排序更受欢迎
  • 原文地址:https://www.cnblogs.com/qisq/p/7595956.html
Copyright © 2011-2022 走看看