Remove substrings inside a list with better than O(n^2) complexity

后端 未结 4 1503
清酒与你
清酒与你 2021-02-02 18:21

I have a list with many words (100.000+), and what I\'d like to do is remove all the substrings of every word in the list.

So for simplicity, let\'s imagine that I have

4条回答
  •  暖寄归人
    2021-02-02 18:52

    You can sort your data by length, and then use a list comprehension:

    words = ['Hello', 'Hell', 'Apple', 'Banana', 'Ban', 'Peter', 'P', 'e']
    new_words = sorted(words, key=len, reverse=True)
    final_results = [a for i, a in enumerate(new_words) if not any(a in c for c in new_words[:i])]
    

    Output:

    ['Banana', 'Hello', 'Apple', 'Peter']
    

提交回复
热议问题