I'm wondering if they're is a "pythonic" way of removing elements from a list, if that element contains a substring of another element.
For example say we have a list like this:
["/var/html/site1", "/var/html/site1/nested_web_root1", "/var/html/site1/nested_web_root2"]
/var/html/site1 is contained within both: /var/html/site1/nested_web_root1 & /var/html/site1/nested_web_root2
As such I'd like them removed from the list.
I've already written a function to do this, and it does mostly work, but the code is just horrible and overly-complex. There are also edge-cases where it just fails.
Here is what I've written so far:
def substringSieve(string_list):
string_list.sort(key=lambda s: len(s), reverse=False)
out = []
bad_list = []
for outer_string in string_list:
for inner_string in string_list:
if outer_string != inner_string:
if outer_string in inner_string:
out.append(outer_string)
bad_list.append(inner_string)
if outer_string not in out and outer_string not in bad_list:
out.append(outer_string)
return out
Can anyone offer some insight?
Thanks.
if inner_string.startswith(outer_string):.