失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > 组合数据类型练习 英文词频统计实例9-21

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

时间:2022-03-08 21:27:03

相关推荐

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

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

>>>score=list('21223113321')>>>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={'洪英杰':93,'郭子维':74,'王五':45,'徐均钧':66}>>>print('学生成绩字典:',d)>>>d['钱二']=92>>>print('增加:',d)>>>d.pop('徐均钧')>>>print('删除:',d)>>>d['张三']=73>>>print('修改:',d)>>>print('查询李四成绩:',d.get('李四','无'))

3、列表,元组,字典,集合的遍历。

>>>ls=list("4613125646")>>>tu=tuple("4613125646")>>>s=set("4613125646")>>>d={'洪英杰':93,'郭子维':74,'王五':45,'徐均钧':66}>>>print("列表:",ls)>>>for i in ls:print(i,end=' ')>>>print("\n")>>>print("元组:",tu)>>>for i in tu:print(i,end=' ')>>>print("\n")>>>print("集合:",s)>>>for i in s:print(i,end=' ')>>>print("\n")>>>print("字典:",d)>>>for i in d:print(i,end='\t')>>>print(d[i],end='\n')

4、英文词频统计实例待分析字符串分解提取单词大小写 txt.lower()分隔符'.,:;?!-_’计数字典排序list.sort()输出TOP(10)

faded = '''You were the shadow to my lightDid you feel us?Another startYou fade awayAfraid our aim is out of sightWanna see usAliveWhere are you now?Where are you now?Where are you now?Was it all in my fantasy?Where are you now?Were you only imaginary?Where are you now?AtlantisUnder the seaUnder the seaWhere are you now?Another dreamThe monster's running wild inside of meI'm fadedI'm fadedSo lost, I'm fadedI'm fadedSo lost, I'm fadedThese shallow waters never met what I neededI'm letting go a deeper diveEternal silence of the sea. I'm breathing aliveWhere are you now?Where are you now?Under the bright but faded lightsYou've set my heart on fireWhere are you now?Where are you now?Where are you now?AtlantisUnder the seaUnder the seaWhere are you now?Another dreamThe monster's running wild inside of meI'm fadedI'm fadedSo lost, I'm fadedI'm fadedSo lost, I'm faded'''faded = faded.lower()for i in '?!,.\'\n':faded = faded.replace(i,' ')words = faded.split(' ')dic={}keys = set(words)for i in keys:dic[i]=words.count(i)c = list(dic.items())c.sort(key=lambda x:x[1],reverse=True)for i in range(10):print(c[i])

建立学生学号成绩字典

>>>score = {10:'41',20:'64',30:'71',40:'81',50:'91'}>>>print(score)>>>score[6] = '90'>>>print(score)>>>score.pop(6)>>>print(score)>>>for i in score:print("{:>2} : {:<2}".format(i,score.get(i)))

列表,元组,字典,集合

>>>ls=list("4613125646")>>>tu=tuple("4613125646")>>>se=set("4613125646")>>>d={'阿一':93,'阿二':74,'阿三':45,'阿四':66}>>>print("列表:",ls)>>>for i in ls:print(i,end=' ')>>>print("\n")>>>print("元组:",tu)>>>for i in tu:print(i,end=' ')>>>print("\n")>>>print("集合:",se)>>>for i in se:print(i,end=' ')>>>print("\n")>>>print("字典:",d)>>>for i in d:print(i,end='\t')print(d[i],end='\n')

歌词统计

>>>song = '''Vincent,Don McLean. Starry starry nightpaint your palette blue and greylook out on a summer\'s daywith eyes that know the darkness in my soul.Shadows on the hillssketch the trees and the daffodilscatch the breeze and the winter chillsin colors on the snowy linen land.And now I understandwhat you tried to say to meand how you suffered for your sanityand how you tried to set them free.They would not listen they did not know howperhaps they\'ll listen now.Starry starry nightflaming flowers that brightly blazeswirling clouds in violet hazereflect in Vincent\'s eyes of China blue.Colors changing huemorning fields of amber grainweathered faces lined in painare smoothed beneath the artist\'s loving hand.And now I understandwhat you tried to say to meand how you suffered for your sanityand how you tried to set them free.They would not listen they did not know howperhaps they\'ll listen now.For they could not love youbut still your love was trueand when no hope was left in sight on thatstarry starry night.You took your life as lovers often do,But I could have told you Vincentthis world was never meant for one as beautiful as you.Starry starry nightportraits hung in empty hallsframeless heads on nameless wallswith eyes that watch the world and can\'t forget.Like the stranger that you\'ve metthe ragged men in ragged clothesthe silver thorn of bloddy roselie crushed and broken on the virgin snow.And now I think I knowwhat you tried to say to meand how you suffered for your sanityand how you tried to set them free.They would not listen they\'re not listening stillperhaps they never will.'''>>>song = song.lower()>>>for i in '?!,.\'\n':>>> song = song.replace(i,' ')>>>words = song.split(' ')>>>dic={}>>>keys = set(words)>>>for i in keys:>>>dic[i]=words.count(i)>>>c = list(dic.items())>>>c.sort(key=lambda x:x[1],reverse=True)>>>for i in range(10):>>>print(c[i])

如果觉得《组合数据类型练习 英文词频统计实例9-21》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。