Determine prefix from a set of (similar) strings

前端 未结 9 2118
我在风中等你
我在风中等你 2020-11-27 14:25

I have a set of strings, e.g.

my_prefix_what_ever
my_prefix_what_so_ever
my_prefix_doesnt_matter

I simply want to find the longest common p

9条回答
  •  眼角桃花
    2020-11-27 15:03

    I had a slight variation of the problem and google sends me here, so I think it will be useful to document:

    I have a list like:

    • my_prefix_what_ever
    • my_prefix_what_so_ever
    • my_prefix_doesnt_matter
    • some_noise
    • some_other_noise

    So I would expect my_prefix to be returned. That can be done with:

    from collections import Counter
    
    def get_longest_common_prefix(values, min_length):
        substrings = [value[0: i-1] for value in values for i in range(min_length, len(value))]
        counter = Counter(substrings)
        # remove count of 1
        counter -= Counter(set(substrings))
        return max(counter, key=len)
    

提交回复
热议问题