26

如果它与子字符串匹配,如何从列表中删除元素?

我尝试使用pop()andenumerate方法从列表中删除一个元素,但似乎我缺少一些需要删除的连续项目:

sents = ['@$\tthis sentences needs to be removed', 'this doesnt',
     '@$\tthis sentences also needs to be removed',
     '@$\tthis sentences must be removed', 'this shouldnt',
     '# this needs to be removed', 'this isnt',
     '# this must', 'this musnt']

for i, j in enumerate(sents):
  if j[0:3] == "@$\t":
    sents.pop(i)
    continue
  if j[0] == "#":
    sents.pop(i)

for i in sents:
  print i

输出:

this doesnt
@$  this sentences must be removed
this shouldnt
this isnt
#this should
this musnt

期望的输出:

this doesnt
this shouldnt
this isnt
this musnt
4

3 回答 3

42

像这样简单的东西怎么样:

>>> [x for x in sents if not x.startswith('@$\t') and not x.startswith('#')]
['this doesnt', 'this shouldnt', 'this isnt', 'this musnt']
于 2012-10-01T02:34:46.783 回答
16

这应该有效:

[i for i in sents if not ('@$\t' in i or '#' in i)]

如果您只想要以指定句子开头的内容,请使用该str.startswith(stringOfInterest)方法

于 2012-10-01T02:37:13.817 回答
14

另一种技术使用filter

filter( lambda s: not (s[0:3]=="@$\t" or s[0]=="#"), sents)

您的原始方法的问题是,当您在列表项上i并确定它应该被删除时,您将其从列表中删除,这会将i+1项目滑入该i位置。您在 index 处的循环的下一次迭代,i+1但该项目实际上是i+2.

说得通?

于 2012-10-01T02:45:16.943 回答