组合数据类型练习,英文词频统计实例
Posted 少钏_leo
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了组合数据类型练习,英文词频统计实例相关的知识,希望对你有一定的参考价值。
1、列表实例:由字符串创建一个作业评分列表,做增删改查询统计遍历操作。例如,查询第一个3分的下标,统计1分的同学有多少个,3分的同学有多少个等
score=list(‘3212233331133213‘) print(‘作业评分列表:‘,score) score.append(‘3‘) print(‘增加:‘,score) score.pop() print(‘删除:‘,score) score.insert(2,‘1‘) print(‘插入:‘,score) score[2]=‘2‘ print(‘修改:‘,score) print(‘第一个3分的下标:‘,score.index(‘3‘)) print(‘1分的个数:‘,score.count(‘1‘)) print(‘3分的个数:‘,score.count(‘3‘))
2、字典实例:建立学生学号成绩字典,做增删改查遍历操作。
d={‘牛三‘:95,‘牛一‘:78,‘牛二‘:65,‘牛牛‘:66} print(‘学生成绩字典:‘,d) d[‘钱二‘]=98 print(‘增加:‘,d) d.pop(‘牛牛‘) print(‘删除:‘,d) d[‘牛三‘]=78 print(‘修改:‘,d) print(‘查询牛一成绩:‘,d.get(‘牛一‘,‘无‘))
3、列表,元组,字典,集合的遍历。
s=list(‘3856132798‘) t=set(‘2581327489‘) 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=‘‘)
4、英文词频统计实例
s=‘‘‘we wish you a merry christmas we wish you a merry christmas we wish you a merry christmas and a happy new year good tidings we bring to you and your kin good tidings for christmas and a happy new year we wish you a merry christmas we wish you a merry christmas we wish you a merry christmas and a happy new year and have yourself a merry little christmas, have yourself a merry little christmas night Jingle Bells (The One Horse Open Sleigh) Lyrics:James Lord Pierpont Music:James Lord Pierpont Dashing through the snow, In a one horse open sleigh, Over the fields we go, Laughing all the way; Bells on bobtails ring, Making spiritis bright, What fun it is to ride and sing a sleighing song to night. Jingle Bells! Jingle Bells! Jingle all the way! Oh, what fun it is to ride in a one horse open sleigh! Jingle Bells! Jingle Bells! Jingle all the way! Oh, what fun it is to ride in a one horse open sleigh! A day or two ago I thought I‘d take a ride, And soon Miss Fannie Bright, Was seated by my side; THe horse was lean and lank, Misfortune seemed his lot, He got into a drifted bank, And we, we got upsot. Jingle Bells! Jingle Bells! Jingle all the way! Oh, what fun it is to ride in a one horse open sleigh! Jingle Bells! Jingle Bells! Jingle all the way! Oh, what fun it is to ride in a one horse open sleigh! Now the ground is white, Go it while you‘re young, Take the girls tonight, And sing this sleighing song. Just get a bobtailed bay, Two-forty for this spead, Then hitch him to an open sleigh, And crack! You‘ll take the lead. Jingle Bells! Jingle Bells! Jingle all the way! Oh, what fun it is to ride in a one horse open sleigh! Jingle Bells! Jingle Bells! Jingle all the way! Oh, what fun it is to ride in a one horse open sleigh!‘‘‘ print("happy出现次数",s.count(‘happy‘)) 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) a=list(dic.items()) a.sort(key=lambda x:x[1],reverse=True) for i in range(10): print(a[i])
s=‘‘‘we wish you a merry christmas we wish you a merry christmas we wish you a merry christmas and a happy new year good tidings we bring to you and your kin good tidings for christmas and a happy new year we wish you a merry christmas we wish you a merry christmas we wish you a merry christmas and a happy new year and have yourself a merry little christmas, have yourself a merry little christmas night Jingle Bells (The One Horse Open Sleigh) Lyrics:James Lord Pierpont Music:James Lord Pierpont
Dashing through the snow, In a one horse open sleigh, Over the fields we go, Laughing all the way; Bells on bobtails ring, Making spiritis bright, What fun it is to ride and sing a sleighing song to night.
Jingle Bells! Jingle Bells! Jingle all the way! Oh, what fun it is to ride in a one horse open sleigh! Jingle Bells! Jingle Bells! Jingle all the way! Oh, what fun it is to ride in a one horse open sleigh!
A day or two ago I thought I‘d take a ride, And soon Miss Fannie Bright, Was seated by my side; THe horse was lean and lank, Misfortune seemed his lot, He got into a drifted bank, And we, we got upsot.
Jingle Bells! Jingle Bells! Jingle all the way! Oh, what fun it is to ride in a one horse open sleigh! Jingle Bells! Jingle Bells! Jingle all the way! Oh, what fun it is to ride in a one horse open sleigh!
Now the ground is white, Go it while you‘re young, Take the girls tonight, And sing this sleighing song. Just get a bobtailed bay, Two-forty for this spead, Then hitch him to an open sleigh, And crack! You‘ll take the lead.
Jingle Bells! Jingle Bells! Jingle all the way! Oh, what fun it is to ride in a one horse open sleigh! Jingle Bells! Jingle Bells! Jingle all the way! Oh, what fun it is to ride in a one horse open sleigh!‘‘‘print("happy出现次数",s.count(‘happy‘))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)a=list(dic.items())a.sort(key=lambda x:x[1],reverse=True)for i in range(10): print(a[i])
以上是关于组合数据类型练习,英文词频统计实例的主要内容,如果未能解决你的问题,请参考以下文章