日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【作业】组合数据类型练习,英文词频统计实例

發布時間:2025/5/22 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【作业】组合数据类型练习,英文词频统计实例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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

總結

以上是生活随笔為你收集整理的【作业】组合数据类型练习,英文词频统计实例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。