Python 哈姆雷特字數統計

2022-06-01 21:48:11 字數 727 閱讀 8290

def gettext():  #賦予txt哈姆雷特**-全英文小寫-全符號變空格-返回txt

txt = open('c:/users/eternal/desktop/hamlet.txt','r').read()  #路徑要選好

txt = txt.lower()

for ch in '!"#$%&()*+,-./:;<=>?@[\\]^_`~':

txt = txt.replace(ch," ")

return txt

hamlettxt = gettext()

words = hamlettxt.split()  #以空字元做列表返回

counts = {}

for word in words:

counts[word] = counts.get(word,0) + 1    #get不到word就建立word為下標的值0+1,如果get到了就在word的值上加1,然後更新字典

items = list(counts.items())            #字典的鍵值對變成列表

items.sort(key=lambda x:x[1],reverse=true)    #以出現次數為標準排列,從大到小

print(items)

for i in range(10):

word,count = items[i]

print(''.format(word,count))

python 哈姆雷特 字數統計 詞云

def gettext 賦予txt哈姆雷特 全英文小寫 全符號變空格 返回txt txt open hamlet.txt r read 注意文字的路徑 txt txt.lower 文字字元全部轉變為小寫字母 for ch in txt txt.replace ch,使用空格字元替換掉文字中所有特殊字...

書籍字數統計

統計始章節和書籍的字數 update chapter set fontcount len cast content as varchar 8000 update book set finishcount pp.fontcount from book inner join select sum fon...

jQuery實現微博字數統計

xhtml class clear id saywrap class saylist href src images user.gif alt class saytxt href demo 發布的內容.class date class clear xhtml是乙個表單,裡面有輸入框textarea,...