Fastest way to check if a value exists in a list

后端 未结 12 2086
猫巷女王i
猫巷女王i 2020-11-22 00:18

What is the fastest way to know if a value exists in a list (a list with millions of values in it) and what its index is?

I know that all values in the list are uniqu

12条回答
  •  花落未央
    2020-11-22 00:44

    This is not the code, but the algorithm for very fast searching.

    If your list and the value you are looking for are all numbers, this is pretty straightforward. If strings: look at the bottom:

    • -Let "n" be the length of your list
    • -Optional step: if you need the index of the element: add a second column to the list with current index of elements (0 to n-1) - see later
    • Order your list or a copy of it (.sort())
    • Loop through:
      • Compare your number to the n/2th element of the list
        • If larger, loop again between indexes n/2-n
        • If smaller, loop again between indexes 0-n/2
        • If the same: you found it
    • Keep narrowing the list until you have found it or only have 2 numbers (below and above the one you are looking for)
    • This will find any element in at most 19 steps for a list of 1.000.000 (log(2)n to be precise)

    If you also need the original position of your number, look for it in the second, index column.

    If your list is not made of numbers, the method still works and will be fastest, but you may need to define a function which can compare/order strings.

    Of course, this needs the investment of the sorted() method, but if you keep reusing the same list for checking, it may be worth it.

提交回复
热议问题