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

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

时间:2017-09-21 21:19:15      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:alt   enter   type   decided   moment   lap   when   修改   alpha   

1.列表实例:由字符串创建一个作业评分列表,做增删改查询统计遍历操作。例如,查询第一个3分的下标,统计1分的同学有多少个,3分的同学有多少个等。

score=list(‘3231233212232313232‘)
print(‘评分表:\n‘,score)

score.append(‘1‘)
print(‘\n增加一个同学的评分:\n‘,score)

score.pop(0)
print(‘\n删除第一个的同学的评分:\n‘,score)

score[1]=0
print(‘\n修改第二个同学的评分为0:\n‘,score)

print(‘\n查找第一个3分的下标:{}‘.format(score.index(‘3‘)))
print(‘1分的人数:{}个‘.format(score.count(‘1‘)))
print(‘3分的人数:{}个‘.format(score.count(‘3‘)))

 运行结果:

技术分享

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

dic={‘小白‘:100,‘小红‘:80,‘小粉‘:90,‘小紫‘:70}
print(‘学生成绩字典:\n‘,dic)

dic[‘小黑‘]=60
print(‘\n增加一个学生小黑及他的成绩:\n‘,dic)

dic.pop(‘小紫‘)
print(‘\n删除学生小紫:\n‘,dic)

dic[‘小红‘]=87
print(‘\n修改学生小红的成绩:\n‘,dic)

print(‘\n查找学生小白的成绩:‘,dic.get(‘小白‘))

 运行结果:

技术分享

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

l=list(‘2354321333431‘)#列表
y=tuple(‘2134354675898‘)#元组
dic={‘1111‘:‘01‘,‘2222‘:‘02‘,‘3333‘:‘03‘}#字典
s=set(l)#集合

print("列表:",l)
for i in l:
    print(i,end=‘ ‘)

print("\n\n元组:",y)
for i in y:
    print(i,end=‘ ‘)

print("\n\n字典:",dic)
for i in dic:
    print(i,end=‘\t‘)
    print(dic[i])
    
print("\n集合:",s)
for i in s:
    print(i,end=‘ ‘)

 运行结果:

技术分享

4.英文词频统计实例

  1. 待分析字符串
  2. 分解提取单词
    1. 大小写 txt.lower()
    2. 分隔符‘.,:;?!-_’
  3. 计数字典
    1. 排除语法型词汇,代词、冠词、连词

  4. 排序list.sort()
  5. 输出TOP(10)
passage=‘‘‘  An old gentleman whose eyesight was failing came to stay in a hotel room with a bottle of wine in each hand. On the wall there was a fly which he took for a nail. So the moment he hung them on, the bottles fell broken and the wine spilt all over the floor. When a waitress discovered what had happened, she showed deep sympathy for him and decided to do him a favour.
 So the next morning when he was out taking a walk in the roof garden, she hammered a nail exactly where the fly had stayed.
 Now the old man entered his room. The smell of the spilt wine reminded him of the accident. When he looked up at the wall, he found the fly was there again! He walked to it carefully adn slapped it with all his strength. On hearing a loud cry, the kind-hearted waitress rushed in. To her great surprise, the poor old man was there sitting on the floor, his teeth clenched and his right hand bleeding!‘‘‘
print(‘短文:\n‘,passage)

passage=passage.upper()
print(‘\n变为大写:\n‘,passage)
passage=passage.lower()
print(‘\n变为小写:\n‘,passage)

for i in ‘,.!‘:
     passage=passage.replace(i,‘ ‘)
print(‘\n所有符号换成空格:\n‘,passage)

words=passage.split(‘ ‘)
keys=set(words)
dic={}

exc={‘an‘,‘a‘,‘to‘,‘in‘,‘on‘,‘of‘,‘at‘,‘was‘,‘and‘,‘the‘,‘‘}
for w in exc:
     keys.remove(w)
     
for i in keys:
    dic[i]=words.count(i)
wc=list(dic.items())
wc.sort(key=lambda x:x[1],reverse=True)

print(‘\n排序输出TOP10:‘)
for i in range(10):
     print(wc[i])

 运行结果:

技术分享

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

标签:alt   enter   type   decided   moment   lap   when   修改   alpha   

原文地址:http://www.cnblogs.com/xings/p/7570171.html

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