【问题标题】:Remove multiple occurrence except for particular values?删除除特定值之外的多次出现?
【发布时间】:2013-12-08 21:05:53
【问题描述】:

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

怎么做?

【问题讨论】:

    标签: python algorithm list find-occurrences


    【解决方案1】:
    seen = set()
    res = []
    for line in textlines:
        if line not in seen:
            res.append(line)
            if not line.strip():
                seen.add(line)
    textlines = res
    

    【讨论】:

      【解决方案2】:

      因为我无法抗拒一个好的代码打高尔夫球:

      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 的回答。如果您打算让人类阅读您的代码,您应该使用它:-)

      【讨论】:

        【解决方案3】:
        new = []
        for line in textlines:
            if line in new and line.strip():
                continue
            new.append(line)
        textlines = new
        

        【讨论】:

          猜你喜欢
          • 2022-01-12
          • 2021-08-29
          • 2021-11-12
          • 2019-05-06
          • 2015-03-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-08-01
          相关资源
          最近更新 更多