Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete substrings from a list of strings

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.

like image 598
user2058724 Avatar asked Mar 06 '14 10:03

user2058724


People also ask

How do I remove a substring from a list?

Use list. remove() to remove a string from a list. Call list. remove(x) to remove the first occurrence of x in the list.


1 Answers

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:])]

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']
like image 118
thefourtheye Avatar answered Sep 24 '22 15:09

thefourtheye