只保留恰好出现一次的行

时间:2020-12-30 13:28:04

标签: python duplicates

假设我有文本文件 test.txt,其中有

010101
001010
010101
000011
111111
010101

我想将输入文件中恰好出现一次的所有行写入新文件并删除所有其他行:

001010
000011
111111

这段代码没有做我想要的,因为它只是将重复的行减少到一行,但并没有完全删除它们:

lines_seen = set()
with open("leadsNoDupes.txt", "w+") as output_file:
    for each_line in open("leads.txt", "r"):
        if each_line not in lines_seen:
            output_file.write(each_line)
            lines_seen.add(each_line)

我该如何正确执行此操作?

1 个答案:

答案 0 :(得分:0)

您需要保持计数:

from collections import Counter


with open("leadsNoDupes.txt", "w+") as output_file:
    lines = list(open("leads.txt", "r"))
    counts = Counter(lines)
    for line in lines:
        if counts[line] == 1:
            output_file.write(line)

这种过度收集信息,因为我们真的不需要知道一条线是出现 2、3 还是 7 次,但它仍然是线性的。

相关问题