组合数据类型练习、英语词频统计

    1. 字典实例:建立学生学号成绩字典,做增删改查遍历操作。
      di={}
      di["001"]=76
      di["002"]=87
      di["003"]=79
      di["004"]=98
      di["005"]=72
      print(di)
      di["006"]=95
      print(di)
      del(di['005'])
      print(di)
      for i in di:
          print(i,di[i])

       

    2. 列表,元组,字典,集合的遍历。
      总结列表,元组,字典,集合的联系与区别。
      w=list('154896')
      e=tuple('564255')
      t=dict(zip(w,e))
      u=set([5,3,8,8,6,1])
      for i in w:
          print(i)
      for i in e:
          print(i)
      for i in t:
          print(i,t[i])
      for i in u:
          print(i)
        列表 元组 字典 集合
      可否读写 可读写 只读 可读写 可读写
      可否重复
      存储方式 键值对(键不可重复) 键(不可重复)
      是否有序
      添加 append   d[key]=value add
               
       
      
      

       

    3. 英文词频统计实例
      1. 待分析字符串
      2. 分解提取单词
        1. 大小写 txt.lower()
        2. 分隔符'.,:;?!-_’
        3. 单词列表
      3. 单词计数字典

        lp='''You hid your skeletons, when I had shown you mine
        you woke the devil that I thought you left behind
        I saw the evidence the crimson soaking through
        ten thousand promises, ten thousand ways to lose...

        And you held It all
        but you were careless to let it fall
        you held it all
        and I was by your side
        Powerless

        I watched you fall apart
        and chased you to the end
        I'm left an empty mess, that words can not defend
        you'll never know what I became
        because of you
        ten thousand promises, ten thousand ways to lose...

        And you held It all
        but you were careless to let it fall'''
        less=lp.lower()
        print(less)
        for i in ",...'":
        lp=less.replace(i," ")
        print(lp)
        power=lp.split()
        print(power)

        dic={}

        for i in power:
        dic[i]= power.count(i)
        lp=list(dic.items())

        print('单词计数字典:',lp)

        )

         

         

         

posted @ 2017-09-26 19:43  100彭楚殷  阅读(123)  评论(0编辑  收藏  举报