组合数据类型练习,英文词频统计实例
Posted 47谢远鹏
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了组合数据类型练习,英文词频统计实例相关的知识,希望对你有一定的参考价值。
1、列表实例:由字符串创建一个作业评分列表,做增删改查询统计遍历操作。例如,查询第一个3分的下标,统计1分的同学有多少个,3分的同学有多少个等。
1 work = list("1232122331231") 2 print(work) 3 4 work.append(‘3‘) 5 print(work[-1]) 6 7 work.pop() 8 print(work[-1]) 9 10 work[-1] = 2 11 print(work[-1]) 12 13 print(work.index(‘3‘)) 14 print(work.count(‘1‘)) 15 print(work.count(‘3‘))
输出结果:
[‘1‘, ‘2‘, ‘3‘, ‘2‘, ‘1‘, ‘2‘, ‘2‘, ‘3‘, ‘3‘, ‘1‘, ‘2‘, ‘3‘, ‘1‘] 3 1 2 2 3 4
2、字典实例:建立学生学号成绩字典,做增删改查遍历操作。
score = {1:‘44‘, 2:‘66‘, 3:‘78‘, 4:‘67‘, 5:‘88‘ } print(score) score[6] = ‘98‘ print(score) score[6] = ‘99‘ print(score) score.pop(6) print(score) for i in score: print("{:>2} : {:<2}".format(i,score.get(i)))
输出结果:
{1: ‘44‘, 2: ‘66‘, 3: ‘78‘, 4: ‘67‘, 5: ‘88‘} {1: ‘44‘, 2: ‘66‘, 3: ‘78‘, 4: ‘67‘, 5: ‘88‘, 6: ‘98‘} {1: ‘44‘, 2: ‘66‘, 3: ‘78‘, 4: ‘67‘, 5: ‘88‘, 6: ‘99‘} {1: ‘44‘, 2: ‘66‘, 3: ‘78‘, 4: ‘67‘, 5: ‘88‘} 1 : 44 2 : 66 3 : 78 4 : 67 5 : 88
3、列表,元组,字典,集合的遍历。总结列表,元组,字典,集合的联系与区别。
s=list(‘456132798‘) t=set(‘4561327489‘) c={‘小明‘:‘1‘,‘小红‘:‘3‘,‘小华‘:‘2‘} x=(1, 2, 3, 4, 5 ) print(‘列表的遍历为:‘,end=‘‘) for i in s: print(i,end=‘‘) print() print(‘元祖的遍历为:‘,end=‘‘) for i in x: print(i,end=‘‘) print() print(‘字典key的遍历为:‘,end=‘‘) for i in c: print(i,end=‘‘) print() print(‘字典value的遍历为:‘,end=‘‘) for i in c.values(): print(i,end=‘‘) print() print(‘数组的遍历为:‘,end=‘‘) for i in x: print(i,end=‘‘)
输出结果:
列表的遍历为:456132798 元祖的遍历为:12345 字典key的遍历为:小明小红小华 字典value的遍历为:132 数组的遍历为:12345
4、英文词频统计实例
- 待分析字符串
- 分解提取单词
- 大小写 txt.lower()
- 分隔符‘.,:;?!-_’
- 计数字典
- 排序list.sort()
- 输出TOP(10)
s=‘‘‘Well I wonder could it be When I was dreaming about you baby You were dreaming of me Call me crazy Call me blind To still be suffering is stupid after all of this time Did I lose my love to someone better And does she love you like I do I do, you know I really really do Well hey So much I need to say Been lonely since the day The day you went away So sad but true For me there‘s only you Been crying since the day the day you went away.!?‘‘‘ print("do出现次数",s.count(‘do‘)) s=s.replace(",","") s=s.replace(‘.‘,‘‘) s=s.replace(‘?‘,‘‘) s=s.replace(‘!‘,‘‘) s=s.replace(‘\n‘,‘‘) s=s.lower() s1=s.split(‘ ‘) key=set(s1) dic={} for i in key: dic[i]=s.count(i) wc=list(dic.items()) wc.sort(key=lambda x:x[1],reverse=True) for i in range(10): print(wc[i])
以上是关于组合数据类型练习,英文词频统计实例的主要内容,如果未能解决你的问题,请参考以下文章