Skip to content Skip to sidebar Skip to footer

Pythonic Way To Find All Potential Longest Sequence

So, I have a list like following potential_labels = ['foo', 'foo::bar', 'foo::bar::baz', 'abc', 'abc::cde::def', 'bleh'] The desired_output = ['foo::bar::baz', 'abc::cde::def', 'b

Solution 1:

Option 1max + groupby should do it.

r = [max(g, key=len) for _, g in \
          itertools.groupby(data, key=lambda x: x.split('::')[0])]

r
['foo::bar::baz', 'abc::cde::def', 'bleh']

Option 2 A much simpler solution would involve the collections.OrderedDict:

from collections import OrderedDict

o = OrderedDict()    
for x in data:
    o.setdefault(x.split('::')[0], []).append(x)

r = [sorted(o[k], key=len)[-1] for k in o]

r
['foo::bar::baz', 'abc::cde::def', 'bleh']

Not exactly a one liner, but what is pythonic is subjective after all.

Solution 2:

You can do a simple list comprehension taking advantage of a condition:

>>> [label forlabelin potential_labels if"\0".join(potential_labels).count("\0{}".format(label))==1]
['foo::bar::baz', 'abc::cde::def', 'bleh']

Post a Comment for "Pythonic Way To Find All Potential Longest Sequence"