2013-12-08 61 views
0

我有一行文本列表: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" 

如何做到這一點?

回答

3
seen = set() 
res = [] 
for line in textlines: 
    if line not in seen: 
     res.append(line) 
     if not line.strip(): 
      seen.add(line) 
textlines = res 
0
new = [] 
for line in textlines: 
    if line in new and line.strip(): 
     continue 
    new.append(line) 
textlines = new 
1

因爲我無法抗拒一個好的代碼高爾夫球:

seen = set() 

[x for x in textlines if (x not in seen or not x.strip()) and not seen.add(x)] 
Out[29]: ['First line\n', 'Second line \n', ' \n', ' \n'] 

這相當於@ hughbothwell的答案。如果您打算讓人類閱讀您的代碼,您應該使用哪一個:-)