from functools import reduce
#一篇txt的统计
def readFile(file_name):
#txt文件用utf-8保存,否则读取出错
f=open(file_name,'r')
y=[]
x=f.readlines()
for line in x:
#extend函数作用为向列表中增加另一个列表中元素,而append为增加单一元素
y.extend(line.split())
f.close()
word_list2=[]
for word in y:
word1 = word
while True:
lastchar=word1[-1:]
if lastchar in [',','.','!','?',';','"',':','(',')','\'']:
#rstrip()默认去除right的空格或括号中的元素(存在时)
word2 = word1.rstrip(lastchar)
word1 = word2
else:
word2 = word1
break
while True:
firstchar = word2[0]
if firstchar in [',','.','!','?',';','"',':','(',')','\'']:
word3 = word2.lstrip(firstchar)
word2 = word3
else:
word3 = word2
break
word_list2.append(word3.lower())
freq_list = []
word_saved = []
for word2 in word_list2:
if not word2 in word_saved:
word_saved.append(word2)
freq_list.append((word2,word_list2.count(word2)))
sorted_list = sorted(freq_list,key=lambda x:x[1],reverse=True)
return sorted_list
def mergeStatic(list1,list2):
word1,num1 = list(zip(*list1))
merge_list = []
for word,num in list2:
if not word in word1:
merge_list.append((word,num))
else:
index = word1.index(word)
merge_list.append((word,num+num1[index]))
word2,num2=zip(*list2)
for word,num in list1:
if not word in word2:
merge_list.append((word,num))
sorted_list = sorted(merge_list,key=lambda x:x[1],reverse=True)
if __name__ == '__main__':
file_list = ['1.txt','3.txt','4.txt']
cc=list(map(readFile,file_list))
word_list = reduce(mergeStatic,cc)
print('最常用单词排行榜')
for word in word_list[0:10]:
#print('%-10s %d'%(word[0],word[1]))
readFile('1.txt')
代码如上,不知道怎么改了.求大神指点
你搞错了一点。