码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-09-26 23:35:03      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:dog   ase   改变   大小   允许   分析字符串   line   values   其他   

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

      技术分享

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

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

      技术分享  

  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+‘\n‘)

#将所有大写转换为小写
s=s.lower()
print(‘全部转换为小写的结果:‘+s+‘\n‘)


#统计单词‘has’出现的次数
count=s.count(‘has‘)
print(‘单词and出现的次数为:‘,count)
print(‘\n‘)

#分隔出单词
s=s.split(‘ ‘)
print(‘分隔结果为:‘,s)

dict={}
for i in s:
   dict[i]=s.count(i)
its=list(dict.items())
print(‘字典元组列表:‘,its,‘\n‘)

its.sort(key=lambda x:x[1],reverse=True)
print(‘排序后出现次数前十的单词:‘)
for i in range(1,11):
   print(its[i])

  技术分享

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

标签:dog   ase   改变   大小   允许   分析字符串   line   values   其他   

原文地址:http://www.cnblogs.com/lhw1997/p/7599190.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!