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
不,Python中没有标准函数。
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 .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.