Python: how to check that if an item is in a list efficiently?

后端 未结 4 1319
盖世英雄少女心
盖世英雄少女心 2020-12-30 15:28

I have a list of strings (words like), and, while I am parsing a text, I need to check if a word belongs to the group of words of my current list.

However, my input

4条回答
  •  有刺的猬
    2020-12-30 15:54

    I'm not clear on why you chose a list in the first place, but here are some alternatives:

    Using a set() is likely a good idea. This is very fast, though unordered, but sometimes that's exactly what's needed.

    If you need things ordered and to have arbitrary lookups as well, you could use a tree of some sort: http://stromberg.dnsalias.org/~strombrg/python-tree-and-heap-comparison/

    If set membership testing with a small number of false positives here or there is acceptable, you might check into a bloom filter: http://stromberg.dnsalias.org/~strombrg/drs-bloom-filter/

    Depending on what you're doing, a trie might also be very good.

提交回复
热议问题