您好我有以下2个列表,我想基本上获得第3个更新列表,如果列表中的任何字符串'错'出现在列表'old'中,它会过滤掉包含它的整个字符串行。即我希望更新的列表等同于“新”列表。
wrong = ['top up','national call']
old = ['Hi Whats with ','hola man top up','binga dingo','on a national call']
new = ['Hi Whats with', 'binga dingo']
答案 0 :(得分:2)
您可以使用filter:
>>> list(filter(lambda x:not any(w in x for w in wrong), old))
['Hi Whats with ', 'binga dingo']
>>> [i for i in old if not any(x in i for x in wrong)]
['Hi Whats with ', 'binga dingo']
如果您对这些问题感到不舒服,请使用如下所示的简单的基于for循环的解决方案:
>>> result = []
>>> for i in old:
... for x in wrong:
... if x in i:
... break
... else:
... result.append(i)
...
>>> result
['Hi Whats with ', 'binga dingo']
答案 1 :(得分:0)
>>> wrong = ['top up','national call']
>>> old = ['Hi Whats with ','hola man top up','binga dingo','on a national call']
>>> [i for i in old if all(x not in i for x in wrong)]
['Hi Whats with ', 'binga dingo']
>>>