I have a list
l = ['abc', 'abcdef', 'def', 'defdef', 'polopolo']
I'm trying to delete strings whose superstring is already in the list. In this case, the result should be:
['abcdef', 'defdef', 'polopolo']
I have written the code:
l=['abc','abcdef','def','defdef','polopolo']
res=['abc','abcdef','def','defdef','polopolo']
for each in l:
l1=[x for x in l if x!=each]
for other in l1:
if each in other:
res.remove(each)
but it doesnt seem to work. I have read that we cannot remove from the list while iterating over it. Hence the copy res
, while l
is my original list.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…