python去除文本中重复的字符串

有一个文本a.txt,要求使用python对其进行处理,去除其中重复的字符串,要求只保留一次,其尽可能快速。
a.txt存放有:
中国
中国
中国
日本
日本
中国
美国
日本


美国
英国

要求生成一个b.txt,其中存放的字符串为:
中国
日本
美国

英国

求大神指导,尽可能快速,谢谢,50分送上。

你的数据都是一行一行的吗?

是的话这样试试

input = open("a.txt", "r").read()()
output = open("b.txt", "w+")

patterns = []
for line in input.split("\n"):
    if line not in patterns:
        print line
        patterns.append(line + "\n")

for pattern in patterns:
    output.write(pattern)
output.close()

 测试了下满足你的输入输出

温馨提示:答案为网友推荐,仅供参考
第1个回答  2018-10-30
output=[]
with open('a.txt') as fp:
    lines=fp.readlines()
    for i in lines:
        if i not in output:
            output.append(i)
with open('b.txt','w',encoding='utf-8') as fp1:
    for i in output:
        fp1.write(i)

虽然已经几年过去了,还是写一下吧。以防刚好有人搜到。

Thx.

相似回答