从字符串列表中删除子字符串 [英] Delete substrings from a list of strings
本文介绍了从字符串列表中删除子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个列表
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']
我已经编写了代码:
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
是我的原始列表.
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.
推荐答案
l=['abc','abcdef','def','defdef','polopolo']
print [j for i, j in enumerate(l) if all(j not in k for k in l[i + 1:])]
# ['abcdef', 'defdef', 'polopolo']
通过对列表进行排序,我们可以稍微加快速度
We can speed it up a very little, by sorting the list before
l = sorted(l, key = len)
print [j for i, j in enumerate(l) if all(j not in k for k in l[i + 1:])]
@Ashwini Chaudhary在注释,如果您想保留重复的字符串,则可以这样做
As @Ashwini Chaudhary mentions in the comments, if you want to retain the duplicate strings, then you can do this
l = ['abc','defghi' 'abcdef','def','defdef','defdef', 'polopolo']
l = sorted(l, key = len)
print [j for i,j in enumerate(l) if all(j == k or (j not in k) for k in l[i+1:])]
# ['defdef', 'defdef', 'polopolo', 'defghiabcdef']
这篇关于从字符串列表中删除子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文