# utf-8 # 打開英文演講的txt文檔 s_words = {'own', 'the', 'and', 'that', 'this', 'it', 'my', 'when', 'but', 'so', 'where', 'an', 'a'} sep = ''',.?!:」'「;][ ’''' with open("dream.txt") as fd: words = fd.readlines() for i in sep: # print(i) words = str(words).replace(i, '\n').lower() tr_words = words.lower().split() # for w in wordset: # print(w) worddit = {} for j in tr_words: worddit[j] = tr_words.count(j) diclist = list(worddit.items()) diclist.sort(key=lambda x: x[1], reverse=True) for d in diclist[0:10]: print(d) wordset = set(tr_words) - s_words tr_words = list(wordset) diclist = list(worddit.items()) diclist.sort(key=lambda x: x[1]) #lamba定義了一個匿名函數,只對x[1]進行排序操做 # for d in diclist[0:10]: # print(d) fd.close()