10.15作业

fo = open('C:\\Users\Administrator\Desktop\song.txt', 'r', encoding='utf-8')
sss = fo.read()
fo.close()
print(sss)
sss=sss.lower()#大写转小写
#print(sss)
sss=sss.replace(',','')#去除标点符号
sss=sss.replace('.','')
sss=sss.replace('?','')
sss=sss.replace(':','')
sss=sss.replace('!','')
sss=sss.lstrip()#去除空格
print(sss)
print("将歌词分隔一个一个的单词为:")
str=sss.split()
print(str)
print("统计每个单词出现的次数为:")
strset=set(str)
abc=dict()
for word in strset:
    abc[word]=str.count(word)#遍历处理
wordlist = list(abc.items())#items格式转换
wordlist.sort(key=lambda x:x[1],reverse=True)     #用lambda函数排序
for i in range (20):
    print (wordlist[i])

  

猜你喜欢

转载自www.cnblogs.com/yysa/p/9789381.html