【作业】组合数据类型练习,英文词频统计实例
生活随笔
收集整理的這篇文章主要介紹了
【作业】组合数据类型练习,英文词频统计实例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1、列表實例:由字符串創建一個作業評分列表,做增刪改查詢統計遍歷操作。例如,查詢第一個3分的下標,統計1分的同學有多少個,3分的同學有多少個等。
1 score = list('012332211') 2 print('分數為:',score) 3 print('1分的同學個數:',score.count('1')) 4 print('3分的同學個數:',score.count('3')) 5 print('第一個3分同學的下標為:',score.index('3')) 6 score.append('0') 7 print('添加一個0分在表的末尾:',score) 8 score.insert(0,'3') 9 print('添加一個3分在表的開頭:',score) 10 score.pop(1) 11 print('刪除第二個分數:',score)2、字典實例:建立學生學號成績字典,做增刪改查遍歷操作。
1 score = {'01':'100','02':'77','03':'88','04':'97','05':'97','05':'96','06':'60','07':'55','08':'90','09':'91','10':'59'} 2 print('成績表:\n',score.items()) 3 score['11']='90' 4 print('添加學號為11的分數:\n',score.items()) 5 score['10']='99' 6 print('修改學號為10的分數為99分:\n',score.items()) 7 print('學號是:',score.keys()) 8 print('分數是:',score.values()) 9 found = input('輸入學號查分數:') 10 print(score.get(found,"沒有該學生的分數!"))?
3、分別做列表,元組,字典,集合的遍歷,并總結列表,元組,字典,集合的聯系與區別。
1 ls = list('123456789') 2 ln = tuple('123456789') 3 s = {'01':'100','02':'99','03':'98','04':'97','05':'96','05':'96','06':'95','07':'98','08':'90','09':'91'} 4 a= set('123456789') 5 print('列表:',ls) 6 print('元組',ln) 7 print('字典',s) 8 print('集合',a) 9 print('\n列表的遍歷:') 10 for i in ls: 11 print(i,' ',end=' ') 12 print('\n元組的遍歷:') 13 for j in ln: 14 print(j,' ',end=' ') 15 print('\n字典的遍歷:') 16 for k in s: 17 print(k,'',end=' ') 18 print('\n集合的遍歷:') 19 for l in a: 20 print(l,'',end=' ')| 屬性 | 列表list | 元祖tuple | 字典dict | 集合set |
| 有序 | 是 (正向遞增/反向遞減) | 是 | 無 | 無 |
| 數據可重復 | 是 | 是 | key值唯一 | 否 |
| 數據可修改 | 是 | 否 | 是 | 是 |
| 特點 | 查詢速度隨內容增加而變慢 占用內存較小 | 表達固定數據項、函數多返回值、 多變量同步賦值、循環遍歷等情況下適用 | 改&查操作速度快,不會因key值增加而變慢。占用內存大,內存浪費多(利用空間成本換時間) | 數據獨立性: 能夠過濾重復參數 |
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
4、英文詞頻統計實例
思路:待分析字符→分解提取單詞→大小寫 txt.lower()→分隔符'.,:;?!-_’ →計數字典→排序list.sort()→輸出TOP(10)。
1 write='''It was Mother’s Day.Sun Zheng thought he should do something for his mother. 2 He decided to help his mother do some housework.After school he went to a shop to buy some food on his way home. 3 When he got home,he did his best to cook some nice food,though he couldn’t do the cooking well. 4 Then he cleaned the room.He felt very tired,but he was very happy. 5 When his father and his mother came back and saw the clean rooms and dishes which weren’t so nice,they were very happy. 6 They had their supper together.His mother said,"Thank you,my child!"''' 7 8 exc={'the','to','and','on','a','was'} 9 write = write.lower() 10 for i in ''',.!?''': #去掉標點符號 11 write = write.replace(i,' ') 12 words = write.split(' ') #分詞,單詞的列表 13 keys = set(words) #出現過單詞的集合,字典的Key 14 dic = {} 15 for w in exc: #排除無意義的單詞 16 keys.remove(w) 17 for i in keys: 18 dic[i]=words.count(i) #單詞出現次數的字典 19 diry = list(dic.items()) #把字典轉換為列表 20 diry.sort(key = lambda x:x[1],reverse = True) #按出現的次數降序排序 21 for i in range(10): 22 print(diry[i])?
?
轉載于:https://www.cnblogs.com/young27/p/7568878.html
總結
以上是生活随笔為你收集整理的【作业】组合数据类型练习,英文词频统计实例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 背水一战 Windows 10 (61)
- 下一篇: K.O. -------- Eclips