我有一个文本行列表:textlines
,它是一个字符串列表(以'\n'
结尾)。
我想删除多次出现的行,不包括那些只包含空格、换行和制表符的行。
换句话说,如果原始列表是:
textlines[0] = "First line\n"
textlines[1] = "Second line \n"
textlines[2] = " \n"
textlines[3] = "First line\n"
textlines[4] = " \n"
输出列表为:
textlines[0] = "First line\n"
textlines[1] = "Second line \n"
textlines[2] = " \n"
textlines[3] = " \n"
怎么做?
最佳答案
seen = set()
res = []
for line in textlines:
if line not in seen:
res.append(line)
if not line.strip():
seen.add(line)
textlines = res
关于python - 删除除特定值外的多次出现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20459075/