Fastest way to search a list in python

后端 未结 4 2279
北荒
北荒 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:25

    "test" in a with a list a will do a linear search. Setting up a hash table on the fly would be much more expensive than a linear search. "test" in b on the other hand will do an amoirtised O(1) hash look-up.

    In the case you describe, there doesn't seem to be a reason to use a list over a set.

提交回复
热议问题