从缺少某些字符串的列表中删除元素 - python
Removing elements from a list that lack certain strings - python
我有一个看起来像这样的大列表:
entries = ["['stuff']...other stuff", "['stuff']...stuff", "['stuff']...more stuff", ...]
我想删除列表中不包含单词"other" 或 [=26]的所有元素=].
我试过了,但它并没有删除我需要的所有元素(只有一些接近尾声的元素):
for e in entries:
if 'other' or 'things' not in e:
entries.remove(e)
print entries
我做错了什么?
您不应该在遍历列表时从列表中删除项目。此外,您的条件语句不符合您的意思:它检查 '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]
正如其他人已经指出的,在您的版本中存在三个主要问题:
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)
您可以使用 列表理解表达式 使用 all(..)
检查子字符串为:
>>> [entry for entry in entries if any(something in entry for something in ["other", "things"])]
这将为您 return 包含 "other" 或 "things" 的新单词列表。
我有一个看起来像这样的大列表:
entries = ["['stuff']...other stuff", "['stuff']...stuff", "['stuff']...more stuff", ...]
我想删除列表中不包含单词"other" 或 [=26]的所有元素=].
我试过了,但它并没有删除我需要的所有元素(只有一些接近尾声的元素):
for e in entries:
if 'other' or 'things' not in e:
entries.remove(e)
print entries
我做错了什么?
您不应该在遍历列表时从列表中删除项目。此外,您的条件语句不符合您的意思:它检查 '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]
正如其他人已经指出的,在您的版本中存在三个主要问题:
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)
您可以使用 列表理解表达式 使用 all(..)
检查子字符串为:
>>> [entry for entry in entries if any(something in entry for something in ["other", "things"])]
这将为您 return 包含 "other" 或 "things" 的新单词列表。