Most efficient way for a lookup/search in a huge list (python)

前端 未结 3 788
深忆病人
深忆病人 2020-12-01 01:51

-- I just parsed a big file and I created a list containing 42.000 strings/words. I want to query [against this list] to check if a given word/string belongs to it. So my qu

3条回答
  •  情深已故
    2020-12-01 02:34

    Don't create a list, create a set. It does lookups in constant time.

    If you don't want the memory overhead of a set then keep a sorted list and search through it with the bisect module.

    from bisect import bisect_left
    def bi_contains(lst, item):
        """ efficient `item in lst` for sorted lists """
        # if item is larger than the last its not in the list, but the bisect would 
        # find `len(lst)` as the index to insert, so check that first. Else, if the 
        # item is in the list then it has to be at index bisect_left(lst, item)
        return (item <= lst[-1]) and (lst[bisect_left(lst, item)] == item)
    

提交回复
热议问题