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

1

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.