gpt4 book ai didi

python : Compare two large files

转载 作者:太空宇宙 更新时间:2023-11-03 15:44:39 25 4
gpt4 key购买 nike

这是 Compare two large files 的后续问题由 phihag 回答

我想在比较两个文件后显示不同的行数。想要在程序完成后显示为消息行数不同

我的尝试:

with open(file2) as b:
blines = set(b)
with open(file1) as a:
with open(file3, 'w') as result:
for line in a:
if line not in blines:
result.write(line)

lines_to_write = []
with open(file2) as b:
blines = set(b)
with open(file1) as a:
lines_to_write = [l for l in a if l not in blines]

print('count of lines are in difference:', len(lines_to_write))

最佳答案

如果可以将所有内容加载到内存中,则可以对集合执行以下操作:

union = set(alines).union(blines)
intersection = set(alines).intersection(blines)
unique = union - intersection

编辑:更简单(也更快)的是:

set(alines).symmetric_difference(blines)

关于 python : Compare two large files,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50735507/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com