Chris Hall Chris Hall -4 years ago 59
Python Question

Reduce list based off of element substrings

I'm looking for the most efficient way to reduce a given list based off of substrings already in the list.

For example

mylist = ['abcd','abcde','abcdef','qrs','qrst','qrstu']

would be reduced to:

mylist = ['abcd','qrs']

because both 'abcd' and 'qrs' are the smallest substring of other elements in that list. I was able to do this with about 30 lines of code, but I suspect there is a crafty one-liner out there..

Answer Source

this seems to be working (but not so efficient i suppose)

def reduce_prefixes(strings):
    sorted_strings = sorted(strings)
    return [element
            for index, element in enumerate(sorted_strings)
            if all(not previous.startswith(element) and
                   not element.startswith(previous)
                   for previous in sorted_strings[:index])]


>>>reduce_prefixes(['abcd', 'abcde', 'abcdef',
                    'qrs', 'qrst', 'qrstu'])
['abcd', 'qrs']
>>>reduce_prefixes(['abcd', 'abcde', 'abcdef',
                    'qrs', 'qrst', 'qrstu',
                    'gabcd', 'gab', 'ab'])
['ab', 'gab', 'qrs']
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download