这似乎是一个微不足道的问题,已经被问到了,但也许会有所帮助。我要指出的是,它与我提供链接comparing two text files and remove duplicates in python的一个问题有关。
问题:我有two.txt文件,同时包含列表中提供的单词(列,约3)。现在,我已经利用了我所附加的脚本,它基于链接中的对话,尽管它实际上没有返回一个文件,这是比较的结果。
让我解释一下:目标是生成一个文件,该文件包含两个文件中的两个单词,但没有重复。
我希望我已经说得足够清楚了,我感谢任何愿意帮助我的人。
有了这个,不符合我的目标
with open("TEXT1.txt") as f1:
set1 = set(f1.readlines())
with open("TEXT2.txt") as f2:
set2 = set(f2.readlines())
nondups = set1 - set2
with open("MERGED.txt", "w") as out:
out.writelines(nondups)发布于 2022-05-17 09:19:50
试试这个:
s1 = {1,2,3,4}
s2 = {3,4,5,6}
print(s1.intersection(s2))输出:{3, 4}
只需将行nondups = set1 - set2更改为nondups = set1.intersection(set2)即可。
发布于 2022-05-17 09:24:30
尝试如下:我对代码进行了相应的注释
# open files a.txt and b.txt and get the content as a list of lines
with open('a.txt') as f:
a = f.readlines()
with open('b.txt') as f:
b = f.readlines()
# get the string from the list
a_str = ''.join(a)
b_str = ''.join(b)
# get sets of unique words
a_set = set(a_str.split(" "))
b_set = set(b_str.split(" "))
# merge sets
c_set = a_set.union(b_set)
# write to a new file
with open('c.txt', 'w') as f:
f.write(' '.join(c_set))https://stackoverflow.com/questions/72271306
复制相似问题