英文词频统计预备,组合数据类型练习
生活随笔
收集整理的這篇文章主要介紹了
英文词频统计预备,组合数据类型练习
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.實例: 下載一首英文的歌詞或文章,將所有,.?!等替換為空格,將所有大寫轉換為小寫,統計某幾個單詞出現的次數,分隔出一個一個的單詞。
str='''Tyler was born infected with HIV: his mother was also infected. From the very beginning of his life, he was dependent on medications to enable him to survive.When he was five, he had a tube surgically inserted in a vein in his chest.This tube was connected to a pump, which he carried in a small backpack on his back. Medications were hooked up to this pump and were continuously supplied through this tube to his bloodstream. At times, he also needed supplemented oxygen to support his breathing.'''#將所有大寫轉換為小寫 str=str.lower() print('全部轉換為小寫的結果:'+str+'\n')#將所有將所有其他做分隔符(,.?!)替換為空格 for i in ',.?!':str=str.replace(i,' ') print('其他分隔符替換為空格的結果:'+str+'\n')#統計單詞‘was’出現的次數 count=str.count('was') print('單詞was出現的次數為:',count)#分隔出一個一個單詞 str=str.split(' ') print('分隔結果為:',str)?
2.列表實例:由字符串創建一個作業評分列表,做增刪改查詢統計遍歷操作。例如,查詢第一個3分的下標,統計1分的同學有多少個,3分的同學有多少個等。
str=list('1231212321323123') print('作業評分列表:',str)#查詢第一個3分的下標 a=str.index('3') print('第一個3分的下標為:',a)#查詢1分的同學有多少個 one=str.count('1') print('1分的同學有:',one)#查詢3分的同學有多少個 three=str.count('3') print('3分的同學有:',three)#追加一個2分的同學 str.append('2') print('追加后的結果為:',str)#修改下標為2的同學的分數為1 str[2]='1' print('修改后的結果為:',str)#在下標為5的位置插入一個分數為3的同學 str.insert(5,'3') print('插入后的結果為:',str)#刪除最后一個同學的分數 str.pop() print('刪除后的結果為:',str)#給評分列表排序 str.sort() print('排序為:',str)?
?
3.簡要描述列表與元組的異同。
list是一種有序的序列,正向遞增、反向遞減序號,可以隨時添加和刪除其中的元素。沒有長度限制、元素類型可以不同。
tuple和list非常類似,但是tuple一旦初始化就不能修改。
?
轉載于:https://www.cnblogs.com/lkm123/p/7574035.html
總結
以上是生活随笔為你收集整理的英文词频统计预备,组合数据类型练习的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: POJ3111 K Best —— 01
- 下一篇: RBAC权限管理