我有一张单子
l = ['abc', 'abcdef', 'def', 'defdef', 'polopolo']
我正在尝试删除其超弦已在列表中的字符串。在这种情况下,结果应为:
['abcdef', 'defdef', 'polopolo']
我已经编写了代码:
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)
但它似乎不起作用。我已经读到,我们不能在遍历列表时将其从列表中删除。因此,副本res
,而l
是我的原始列表
我们可以在之前对列表进行排序,从而加快速度
作为@Ashwini Chaudhary mentions in the comments,如果希望保留重复的字符串,那么可以这样做
相关问题 更多 >
编程相关推荐