将 4500 万行文本文件与大约 20 万行文本文件进行比较并从较小的文件中生成不匹配项的最有效方法是什么?

What is the most efficient way to compare 45 Million rows of Text File to about 200k rows text file and produce non matches from the smaller file?

我有一个包含哈希的 4500 万行 txt 文件。将文件与另一个文件进行比较并仅提供第二个文件中不在大型 txt 文件中的项目的 most 有效方法是什么?

当前工作:

comm -13 largefile.txt smallfile.txt >> newfile.txt 

这工作得非常快,但我希望将它推入 python 到 运行 而不管 os?

尝试内存问题:

tp = pd.read_csv(r'./large file.txt',encoding='iso-8859-1', iterator=True, chunksize=50000)
full = pd.concat(tp, ignore_index=True)`

此方法会占用内存,但由于某些原因通常会出错。

示例:

<large file.txt>
hashes
fffca07998354fc790f0f99a1bbfb241
fffca07998354fc790f0f99a1bbfb242
fffca07998354fc790f0f99a1bbfb243
fffca07998354fc790f0f99a1bbfb244
fffca07998354fc790f0f99a1bbfb245
fffca07998354fc790f0f99a1bbfb246


<smaller file.txt>
hashes
fffca07998354fc790f0f99a1bbfb245
fffca07998354fc790f0f99a1bbfb246
fffca07998354fc790f0f99a1bbfb247
fffca07998354fc790f0f99a1bbfb248
fffca07998354fc790f0f99a1bbfb249
fffca07998354fc790f0f99a1bbfb240

预期输出

<new file.txt>
hashes
fffca07998354fc790f0f99a1bbfb247
fffca07998354fc790f0f99a1bbfb248
fffca07998354fc790f0f99a1bbfb249
fffca07998354fc790f0f99a1bbfb240

哈希table。或者在 Python 术语中,只需使用 set.

smaller 文件中的每个项目放入集合中。 200K 项完全没问题。枚举较大文件中的每个项目以查看它是否存在于较小文件中。如果匹配,则从散列 table.

中删除该项

完成后,集合中剩余的任何项目表示在较大文件中找不到的项目。

我的 Python 有点生疏,但它会变成这样:

s = set()

with open("small_file.txt") as f:
     content = f.readlines()

for line in content:
    line = line.strip()
    s.add(line)

with open("large_file.txt") as f:
    for line in f:
         if line in s:
            s.discard(line.strip())

for i in s:
    print(i)

尚未测试,但我认为这不会占用大量内存(不知道速度):

unique = []

with open('large_file.txt') as lf, open('small_file.txt') as sf:
    for small_line in sf:
        for large_line in lf:
            if small_line == large_line:
                break
        else:
            unique.append(small_line)
        lf.seek(0)

答案最终是一个白痴检查,应该在我发布之前就进行了很好的检查。

我的 IDE 是 运行 32 位 python 而不是 64 位,因为我必须进行重新安装。进行此更改后,所有文件都可以很好地一次加载所有文件,并且 运行 在数据帧上连接并删除重复项。感谢您的所有回答以及您提供的意见和帮助。谢谢。