删除与另一个列表中的任何条目匹配的列表条目

ewh*_*itt 1 python

我有一个我希望与其他列表匹配的诅咒词列表以删除匹配.我通常会单独使用list.remove('entry'),但是循环遍历另一个列表中的条目列表 - 然后删除它们让我感到难过.有任何想法吗?

Fac*_*sco 10

使用filter:

>>> words = ['there', 'was', 'a', 'ffff', 'time', 'ssss']
>>> curses = set(['ffff', 'ssss'])
>>> filter(lambda x: x not in curses, words)
['there', 'was', 'a', 'time']
>>> 
Run Code Online (Sandbox Code Playgroud)

它也可以通过列表理解来完成:

>>> [x for x in words if x not in curses]
Run Code Online (Sandbox Code Playgroud)

  • 使`curses`列表是一个坏主意 - `in`对于一个集合是O(1),对于列表是O(n). (3认同)