Removendo um item da lista que corresponde a uma substring
Como faço para remover um elemento de uma lista se ele corresponder a uma substring?
Eu tentei remover um elemento de uma lista usando opop()
eenumerate
método, mas parece que estou perdendo alguns itens contíguos que precisam ser removidos:
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
Saída:
this doesnt
@$ this sentences must be removed
this shouldnt
this isnt
#this should
this musnt
Saída desejada:
this doesnt
this shouldnt
this isnt
this musnt