如果它与子字符串匹配,如何从列表中删除元素?
我尝试使用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