python : Compare two large files

标签 python large-data large-files

这是 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/

相关文章:

python - 如何使用 django-tables2 显示图像

Python,将打印输出附加到Excel文件

php - 处理超大阵列的最佳实践? D B?

javascript - jqgrid海量数据加载问题

R terra函数分类创建非常大的文件

python - 检查python下是否存在postgresql表(可能还有Psycopg2)

php - 在 python 中反序列化 PHP 数据

c++ - 大量对象的极端内存使用

c++ - 人们是如何快速浏览 LinkedIn 密码文件的?

php - 如何逐行处理大型 CSV 文件?