如何比较python中两个文本文件的词频?例如,如果一个单词同时包含在file1和file2中,那么它应该只写一次,但是在比较时不加上它们的频率,它应该是{'The':3,5}。这里3是文件1中的频率,5是文件2中的频率。如果某些单词只存在于一个文件中,而不是同时存在于两个文件中,那么对于该文件,应该有0。请帮忙 以下是我目前所做的:
import operator
f1=open('file1.txt','r') #file 1
f2=open('file2.txt','r') #file 2
wordlist=[]
wordlist2=[]
for line in f1:
for word in line.split():
wordlist.append(word)
for line in f2:
for word in line.split():
wordlist2.append(word)
worddictionary = {}
for word in wordlist:
if word in worddictionary:
worddictionary[word] += 1
else:
worddictionary[word] = 1
worddictionary2 = {}
for word in wordlist2:
if word in worddictionary2:
worddictionary2[word] += 1
else:
worddictionary2[word] = 1
print(worddictionary)
print(worddictionary2)
您可能会发现以下演示程序是获取文件词频的良好起点:
特别是,您需要使用
get_freq
函数来获得一个Counter
对象,它告诉您单词的频率是什么。您的程序可以使用不同的文件名多次调用get_freq
函数,您应该会发现Counter
对象与您以前使用的词典非常相似。在编辑:我误解了问题,代码现在适用于您的问题。在
添加第一个文件单词,如果该单词在第二个文件中,则在值中添加一个。 然后,检查每个单词,如果它的值为0,则删除它。在
这不能通过遍历字典来实现,因为它在遍历字典的同时改变了大小。在
以下是对多个文件(更复杂)的实现方法:
^{pr2}$编辑:以下是对任何文件列表执行此操作的更一般的方法(注释中的解释):
按照您编写的代码,以下是如何创建组合字典:
^{pr2}$相关问题 更多 >
编程相关推荐