从python中的字符串中删除重复的行 [英] Remove duplicate lines from a string in python

查看:104
本文介绍了从python中的字符串中删除重复的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 python 中有一个字符串,想删除重复的行(即当 \n 之间的文本相同时,然后删除第二(第三、第四)次出现,但保留字符串的顺序.例如

I have a string in python, and would like to remove duplicate lines (i.e. when the text between \n is the same, then remove the second (third, fourth) occurrence, but preserve the order of the string. for example

line1 \n line2 \n line3 \n line2 \n line2 \n line 4

会回来:

line1 \n line2 \n line3 \n line 4

我在stackoverflow上看到的其他例子是在将文本文件读入python的阶段处理的(例如使用readline(),查看是否已经在一组读取的行中,然后仅当它是唯一的时才添加到字符串).在我的例子中,这不起作用,因为自从加载到 python 以来,我已经对字符串进行了大量操作......而且它似乎非常拙劣,例如将整个字符串写入txt文件,然后逐行读取以查找重复行

Other examples i have seen on stackoverflow deal with at the stage of reading the text file into python (e.g. using readline(), seeing if already in a set of read in lines, and then adding to string only if it is unique). in my instance this doesn't work, as the string I have has already been heavily manipulated since loading into python... and it seems very botched to e.g. write the whole string to a txt file, and then read in line-by-line looking for duplicated lines

推荐答案

对于 Python 2.7+,这可以通过单行来完成:

For Python 2.7+, this can be done with a one-liner:

from collections import OrderedDict

test_string = "line1 \n line2 \n line3 \n line2 \n line2 \n line 4"

"\n".join(list(OrderedDict.fromkeys(test_string.split("\n"))))

这给了我:'line1 \n line2 \n line3 \n line 4'

这篇关于从python中的字符串中删除重复的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆