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

    1. 字典实例:建立学生学号成绩字典,做增删改查遍历操作。
      mydict={'201508030012':86,'201508030056':98,'201508030065':56}
      #增
      mydict['201508030098']=97
      print("增加后的:",'
      ',mydict,'
      ')
      
      #删
      del(mydict['201508030012'])
      print("删除后的:",'
      ',mydict,'
      ')
      
      #改
      mydict['201508030056']=100
      print("修改后的:",'
      ',mydict,'
      ')
      
      #查
      print('查找201508030065:','
      ',mydict.get('201508030065'),'
      ')
      

        

    2. 列表,元组,字典,集合的遍历。

      #列表,元组,字典,集合的遍历。
      
      #列表的遍历
      p = list('201508030026')
      print('列表的遍历',p,'
      ')
      
      
      #元组的遍历
      t = tuple('201508030026')
      print('元组的遍历',t,'
      ')
      
      
      #集合的遍历
      s = set('201508030026')
      print('集合的遍历:',s,'
      ')
      
      
      #字典的遍历
      dic=()
      name=['lily','perter','miko']
      score=[546,975,556]
      y=dict(zip(name,score))
      print('字典的遍历:',y,'
      ')
      

          

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

    1.列表(List)

      list是处理一组有序项目的数据结构,列表中的元素应该包括在方括号中。在list中,可以添加任意类型的对象,甚至可以是list,实际上list就是一种特殊的对象。

      2.元组(Tuple)

      元组跟列表有相似之处,但不同的是,元组是不可变的。

      3.字典(Dictionary)

      一个字典就好比地址簿一样,你可以通过一个人的名字(keys)去得到他或她的详细信息(values)。注意,键值(key)必须唯一,并且键值只能使用不可改变的对象(像字符串),但可以使用可变或不可          变的对象作为字典的值。换句话说,应该使用简单的对象作为键值。

           4.集合(Set)

      Set也是用{},只是内部的元素不允许重复,也无序。

     

    3.

    1. 待分析字符串
    2. 分解提取单词
      1. 大小写 txt.lower()
      2. 分隔符'.,:;?!-_’
      3. 单词列表
    3. 单词计数字典
    s='''Ankara has built a strong relationship with the Iraqi Kurds through an oil pipeline that feeds the Kurdish economy and Turkey's energy needs. And the authorities in Irbil oppose the PKK Kurdish militant group, allowing Turkish military bases in northern Iraq. Mr Erdogan warned he could close the oil valves in Turkey - but it has not yet happened.'''
    
    #将所有将所有其他做分隔符(,.?!)替换为空格
    for i in ',.?!':
        s=s.replace(i,' ')
    print('其他分隔符替换为空格的结果:'+s+'
    ')
    
    #将所有大写转换为小写
    s=s.lower()
    print('全部转换为小写的结果:'+s+'
    ')
    
    
    #统计单词‘has’出现的次数
    count=s.count('has')
    print('单词and出现的次数为:',count)
    print('
    ')
    
    #分隔出单词
    s=s.split(' ')
    print('分隔结果为:',s)
    
    dict={}
    for i in s:
       dict[i]=s.count(i)
    its=list(dict.items())
    print('字典元组列表:',its,'
    ')
    
    its.sort(key=lambda x:x[1],reverse=True)
    print('排序后出现次数前十的单词:')
    for i in range(1,11):
       print(its[i])
    

      

  • 相关阅读:
    IntelliJ IDEA使用心得之问题篇;
    IntelliJ IDEA使用心得之Maven项目篇
    IntelliJ IDEA使用心得之非Maven项目篇
    IntelliJ IDEA使用心得之插件篇
    IntelliJ IDEA使用心得之快捷键篇
    新博客地址
    【转载】Dijkstra算法和Floyd算法的正确性证明
    【转载】最小生成树之Kruskal算法
    论自动AC机
    【转载】C++ STL priority_queue用法
  • 原文地址:https://www.cnblogs.com/lhw1997/p/7599190.html
Copyright © 2011-2022 走看看