0

I have a list of strings

These strings are made up of strings that will be contained within other strings

And Strings that are Unique in the longest iteration

For example in my list might have the following

4|131
4|201
4|131|2644
4|131|2644|547
4|131|2644|1482
2644

I would like to be able to reduce this down to the longest unique entities

4|201
4|131|2644|547
4|131|2644|1482
2644

I was wondering if there is a standard function in python that can do this process

Jack Walker
  • 238
  • 1
  • 2
  • 10
  • Possible duplicate of [Longest strings from list](https://stackoverflow.com/questions/13964637/longest-strings-from-list) – Macio Oct 25 '18 at 10:31

2 Answers2

3

No single function, but it's pretty easy to build one yourself:

lst = sorted(lst)
longests = [lst[0]]
for item in lst:
    if item.startswith(longests[-1]):
        longests[-1] = item
    else:
        longests.append(item)

print(longests)

another approach:

from operator import itemgetter
from itertools import groupby

class adjacent:
    def __init__(self, func, init=None):
        self.func = func
        self.last = init

    def key(self, value):
        if not self.func(self.last, value):
            self.last = value
        return self.last

slst = sorted(lst, reverse=True)
groups = groupby(slst, adjacent(str.startswith, "").key)
longests = map(itemgetter(0), groups)

print(list(longests))

Note the above implementation considers "4|1" to be a prefix of "4|131" because it uses string matching. If you want to match only with entire strings between the pipes, you'll just have to split on the pipes first and replace with a startswith for list.

Lie Ryan
  • 62,238
  • 13
  • 100
  • 144
2

No, there isn't a standard function in Python.

Peter Wood
  • 23,859
  • 5
  • 60
  • 99