我有两个文本文档,它们基本上都包含相同的单词,但也有一些例外。如何在document2中找到document1中没有的单词并将其打印出来?例如:
文件1: “你好,你好”
文件2: “嗨,你好吗,约翰”
期望输出: “嗨,今天约翰”
编辑:我想打印只在document2中出现但在document1中找不到的单词。我不想把它们之间相同的单词打印出来。在
我创建了这段代码,我认为它可以在两个文本文件之间找到匹配项,但这并不是我想要它做的:
doc1 = open("K:\System Files\Desktop\document1.txt", "r+")
doc2 = open("K:\System Files\Desktop\document2.txt", "r+")
list1 = []
list2 = []
for i in doc1: #Removes the new line after each word
i = i[:-1]
list1.append(i)
for i in doc2:
i = i[:-1]
list2.append(i)
for i in list1:
for j in list2:
if i == j:
print(i)
如果您不担心单词的顺序,则可以使用集合来完成以下操作:
这将显示:
^{pr2}${1}在两个集合之间使用{1}的效果。在
相关问题 更多 >
编程相关推荐