1. 程式人生 > >10.15作業

10.15作業

ever set lambda imp .so ould 英文 this lac

1.英文

import string
fo=open(‘song.txt‘,‘r‘,encoding="utf-8")  #導入
song =fo.read().lower() # 全部變成小寫
fo.close()

print(song)
sep=‘‘‘ .!;?-_:‘‘‘
for quchu in sep:
    song =song.replace(quchu,‘ ‘)

strlist = song.split()
print(len(strlist),strlist)
strset = set(strlist)

noMean ={‘i‘,‘are‘,‘for‘,‘a‘,‘the‘,"i‘ve",‘on‘,‘of‘,‘at‘,‘if‘,"i‘d",‘to‘,‘it‘,‘do‘,‘not‘,‘wouldn t‘,‘what‘,‘up‘,‘or‘,‘but‘,‘this‘,‘one s‘,‘could‘,‘and‘}
strset =strset-noMean
print(len(strset),strset)

strdist = {}
for word in strset:
    strdist[word] = strlist.count(word)
print(len(strdist),strdist)

for word in strset:
    strdist[word] = strlist.count(word)
print(len(strdist),strdist)

wordlist = list(strdist.items())
wordlist.sort(key=lambda x:x[1],reverse=True)
print(strlist)

for i in range(20):
    print(wordlist[i])

  

10.15作業