我有一个大型列表,看起来像这样:
entries = ["['stuff']...other stuff", "['stuff']...stuff", "['stuff']...more stuff", ...]
我想删除列表中不包含“其他”或“这些”字样的所有元素。
我尝试了这个,但它没有删除我需要它的所有元素(只有一些接近结尾):
for e in entries:
if 'other' or 'things' not in e:
entries.remove(e)
print entries
我做错了什么?
答案 0 :(得分:1)
在迭代列表时,您不应该从列表中删除项目。此外,您的条件语句并不符合您的意思:它会检查'other'
的真实性,并仅'things'
进行遏制。要解决此问题,请使用and
和两个单独的in
支票。
如果列表不是很大,你可以使用列表推导来重建它:
entries = [e for e in entries if "other" not in e and "things" not in e]
否则,从列表末尾循环到开头并按索引删除项目。
for i in range(len(entries)-1, -1, -1):
if "other" in entries[i] and "things" in entries[i]:
del entries[i]
答案 1 :(得分:0)
正如其他人已经指出的那样,在你的版本中有三个主要问题:
for e in entries:
if 'other' or 'things' not in e: #or returns first truthy value, and `if other` is always true. Also, you need and, not or.
entries.remove(e) #mutating the item you are iterating over is bad
print entries
以下是您修改上述问题的修订版本:
for e in words[:]: #words[:] is a copy of words, solves mutation issue while iterating
if 'other' not in e and 'things' not in e: #want words that both don't contain 'other' AND dont contain 'things'
print(e)
words.remove(e)
print(words)
以下是一些替代方法:
import re
words = ['this doesnt contain chars you want so gone',
'this contains other so will be included',
'this is included bc stuff']
answer = list(filter(lambda x: re.search('other|stuff',x),words))
other_way = [sentence for sentence in words if re.search('other|stuff',sentence)]
print(answer)
print(other_way)
答案 2 :(得分:0)
您可以使用all(..)
使用列表理解表达式来检查子字符串:
>>> [entry for entry in entries if any(something in entry for something in ["other", "things"])]
这将返回包含"其他"的新单词列表。或"事物"。