Py学习  »  Python

在Python中,并非所有副本都会从文本文件中删除

pinkkdeerr • 3 年前 • 1592 次点击  

我是Python新手。我正在尝试通过执行以下操作从文本文件中删除重复项:

line_seen = set()

f = open('a.txt', 'r')
w = open('out.txt', 'w')

for i in f:
    if i not in line_seen:
            w.write(i)
            line_seen.add(i)

f.close()
w.close()

在我最初的文件中

hello
world
python
world
hello

在我得到的输出文件中

hello
world
python
hello

因此,它没有删除最后一个副本。有谁能帮我理解为什么会发生这种事,我该如何解决它?

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/129280
 
1592 次点击  
文章 [ 4 ]  |  最新文章 3 年前