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

    1. 字典实例:建立学生学号成绩字典,做增删改查遍历操作。

       

    2. dic={}
      d={'01':'2','02':'3'}
      print(d)
      d['03']='01'
      d['04']='03'
      d['05']='02'
      print(d)
      d['02']='01'
      print(d)
      d.pop('04')
      print(d)
      del(d['05'])
      print(d)
      d.keys()
      print(d.keys())
      print(d.values())
      print(d.get('04','无此值'))
      print(d.items())

    3. 列表,元组,字典,集合的遍历。
      ls=list('2312')
      print(ls)
      for i in ls:
          print(i)
      
      tu=tuple('2312')
      print(tu)
      for i in tu:
             print(i)
             
      se=set('2312')
      print(se)
      for i in se:
          print(i)
      
      d={'01':'2','02':'3','03':'1','04':'2'}
      print(d)
      for i in d:
          print(i,d[i])

       


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

      列表和元组里都有重复值,都可以查询。
      列表里的元素可以修改,元组里的元素不能修改。
      集合中没有重复值。
      字典里没有重复的键,但是有重复的值。

      3. 英文词频统计实例

      1. 待分析字符串
      2. 分解提取单词
        1. 大小写 txt.lower()
        2. 分隔符'.,:;?!-_’
        3. 单词列表
      3. 单词计数字典
        news='''The magic of the iPhone brand seems to be fading in China as consumers in the world's largest smartphone market greeted Apple Inc's latest handsets with less than the usual level of enthusiasm.
        
        The relatively weak response came as the country's homegrown brands, such as Huawei Technologies Co Ltd and Xiaomi Corp, are pushing up-market, experts said.
        
        On Monday, in Beijing's Sanlitun Soho, the capital's popular shopping area, Apple's flagship store experienced a slimmer turnout with only one short line of people queuing outside the store to get a try for iPhone 8 or iPhone 8 Plus, which went on sale on Friday.
        
        Inside the store, there were crowds of customers surrounding the desk, but many were there to test the models rather than expressing a commitment to buy. And the store still had plenty of space for more consumers.
        
        The response was in stark contrast to previous years, when hundreds of people hustled and bustled in the stores and queues of people circled around outside.
        
        A security guard at the Apple store said: "The lines are far shorter than those of previous years. I had always been extremely busy when new flagship gadgets were released, but I am apparently not so occupied this year."
        
        Also, not many scalpers were seen outside the store and they are not enthusiastic this year as they didn't rush to solicit businesses.
        
        "We didn't sell much today, even with 100 or 200 yuan ($15-30) off," said a scalper outside an Apple store.
        
        Jia Mo, an analyst at global consultancy Canalys, said consumers have less enthusiasm toward the new iPhone 8 this year. One of the reasons is that local rivals including Huawei, Oppo, Vivo and Xiaomi can give users a better experience by offering high-end features with a lower price," he added.
        
        "Another reason is that consumers are waiting for iPhone X, the tenth-anniversary edition iPhone. When they already have a high-price device, they prefer to wait for the best one, rather than buying one that is brand new but not the latest," he added.
        
        Xiang Ligang, CEO of telecom industry website Cctime, said the iPhone is still one of the most preferable high-end devices in China, though far less popular than before, amid mounting competition from local players.
        
        Charlie Zhang, who bought an iPhone 8 on Monday, said he opted out of domestic smartphones, because they are running on Android systems that are easy to crash.
        
        Li Jiayue contributed to the story.
        
        Contact the writers at chengyu@chinadaily.com.cn'''
        news=news.lower()
        for i in ',.""':
            news=news.replace(i,' ')
            print(news)
            
        words=news.split(' ')
        print(words)

         

         

posted @ 2017-09-26 11:52  092曹馨文  阅读(109)  评论(0编辑  收藏  举报