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

后端 未结 4 1550
清酒与你
清酒与你 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条回答
  •  萌比男神i
    2021-02-02 18:58

    Note that using for is slow in python in general, (you may use numpy arrays or NLP package), aside from that, how about this:

    words = list(set(words))#elimnate dublicates
    str_words = str(words)
    r=[]
    for x in words:
        if str_words.find(x)!=str_words.rfind(x):continue
        else:r.append(x)
    print(r)
    

    as I am answering here, I don't see a reason why c++ wouldn't be an option

提交回复
热议问题