如何消除python函数中的重复行?
让我们将给定的文本文件命名为bar.txt
我们在python中使用文件处理方法来删除python文本文件或函数中的重复行。文本文件或函数必须与python程序文件位于同一目录中。以下代码是删除文本文件bar.txt中重复项并将输出存储在foo.txt中的一种方法。这些文件应与python脚本文件位于同一目录中,否则它将无法正常工作。
文件bar.txt如下
A cow is an animal. A cow is an animal. A buffalo too is an animal. Lion is the king of jungle.
示例
下面的代码删除bar.txt中的重复行,并将其存储在foo.txt中
# This program opens file bar.txt and removes duplicate lines and writes the
# contents to foo.txt file.
lines_seen = set() # holds lines already seen
outfile = open('foo.txt', "w")
infile = open('bar.txt', "r")
print "The file bar.txt is as follows"
for line in infile:
print line
if line not in lines_seen: # not a duplicate
outfile.write(line)
lines_seen.add(line)
outfile.close()
print "The file foo.txt is as follows"
for line in open('foo.txt', "r"):
print line输出结果
文件foo.txt如下
A cow is an animal. A buffalo too is an animal. Lion is the king of jungle.
热门推荐
10 送礼祝福语男性生日简短
11 诗意新婚祝福语高级简短
12 牛年新年祝福语大全简短
13 上司生日祝福语简短 女性
14 道教生日祝福语简短大全
15 舞台上祝福语大全简短
16 化学老师元旦祝福语简短
17 新婚祝福语简短句子
18 恭喜新郎父母简短祝福语