组合数据类型练习,英文词频统计实例9-21
生活随笔
收集整理的這篇文章主要介紹了
组合数据类型练习,英文词频统计实例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 light Did you feel us? Another start You fade away Afraid our aim is out of sight Wanna see us Alive Where 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? Atlantis Under the sea Under the sea Where are you now? Another dream The monster's running wild inside of me I'm faded I'm faded So lost, I'm faded I'm faded So lost, I'm faded These shallow waters never met what I needed I'm letting go a deeper dive Eternal silence of the sea. I'm breathing alive Where are you now? Where are you now? Under the bright but faded lights You've set my heart on fire Where are you now? Where are you now? Where are you now? Atlantis Under the sea Under the sea Where are you now? Another dream The monster's running wild inside of me I'm faded I'm faded So lost, I'm faded I'm faded So 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])?
轉載于:https://www.cnblogs.com/yingja-hong/p/7567595.html
總結
以上是生活随笔為你收集整理的组合数据类型练习,英文词频统计实例9-21的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 关于搭建php电商环境时缺少filein
- 下一篇: 路径数