第四次作業

2022-04-28 00:46:56 字數 2681 閱讀 1223

**詞頻統計:

**:

1

#usr/bin/env/python2#

-*- coding:utf-8 -*-34

import

jieba

5import

jieba.posseg as psg

67 with open("

nottrustanyone.txt

", 'r'

) as f:

8 nottrustanyone =f.read()910

#去掉非漢字的字元

11for ch in

nottrustanyone:

12if ch.isalpha() is

false:

13 nottrustanyone = nottrustanyone.replace(ch, "")14

1516

#分詞並轉成乙個列表

17 nottrustanyonelist = [x.word for x in psg.cut(nottrustanyone) if x.flag.startswith('n'

)]1819#

詞頻統計,用字典儲存,並按出現次數降序排序

20 myset =set(nottrustanyonelist)

2122 keylist =

23 valuelist =

2425

for word in

myset:

2627

2829 wordcount =dict(zip(keylist, valuelist))

3031

#字典排序函式(並取top20):

3233

34def

sortdict(mydict):

35 templist =list()

36for i in

mydict.items():

3738 templist.sort(key=lambda x: x[1], reverse=true)

39 mydict = dict(templist[0:21])

40return

mydict

4142

43 wordcount =sortdict(wordcount)

4445#輸出

46print("

單詞 出現次數

".center(13))

47for word in

wordcount.keys():

48print(word.center(13), wordcount[word])

截圖(top20):

英文歌曲頻次統計:

**:

with open("

halloffame.txt

", 'r'

) as f:

halloffame =f.read().lower()

#去除標點符號,並按行分隔

sep = "

,.!、!@#$%^'

"for ch in

sep:

halloffame = halloffame.replace(ch, ""

)halloffamelist = halloffame.split("\n"

)#對每行用空格分隔

temp =

for i in

halloffamelist:

temp.extend(i.split(""

))halloffamelist = list(x for x in temp if x != ''

)print

(halloffamelist)

#詞頻統計,用字典儲存,並按出現次數降序排序

myset =set(halloffamelist)

keylist =

valuelist =

for word in

myset:

wordcount =dict(zip(keylist, valuelist))

#字典排序函式(並取top20):

defsortdict(mydict):

templist =list()

for i in

mydict.items():

templist.sort(key=lambda x:x[1], reverse=true)

mydict = dict(templist[0:21])

return

mydict

wordcount =sortdict(wordcount)#輸出

print("

單詞 出現次數

".center(13))

for word in

wordcount.keys():

print(word.center(13),wordcount[word])

截圖(top20):

歌曲和**檔案見附件1

第四次作業

扎ogu 典型產品 最高傳輸速率 ieee 802.11a wi fi5 802.11a 43m 450 zyxel p334u 54mbps 1500 zyxel p335u 54mbps 1600 ieee 802.11b d link di 624 a 54mbps 215 linksys w...

第四次作業

作業題一 vs2012 rc在介面上,比beta版更容易使用,彩色的圖示和按照開發 執行 除錯等環境區分的顏色方案讓人愛不釋手。vs2012整合了asp.net mvc 4,全面支援移動和html5,wf 4.5相比wf 4,更加成熟,期待已久的狀態極工作流回來了,更棒的是,現在它的設計器已經支援c...

第四次作業

專案一求1000以內所有偶數的和 includevoid main cout sum includevoid main while i 1000 cout sum includeint main while i 1001 cout 專案3 乘法口訣表 程式設計序,輸出乙個乘法口訣表,形如 1x1 1...