Fastest way to search a list in python

后端 未结 4 2278
北荒
北荒 2020-11-27 18:17

When you do something like \"test\" in a where a is a list does python do a sequential search on the list or does it create a hash table representa

4条回答
  •  执笔经年
    2020-11-27 18:30

    Also note that the list of values I'll have won't have duplicate data and I don't actually care about the order it's in; I just need to be able to check for the existence of a value.

    Don't use a list, use a set() instead. It has exactly the properties you want, including a blazing fast in test.

    I've seen speedups of 20x and higher in places (mostly heavy number crunching) where one list was changed for a set.

提交回复
热议问题