How do Python dictionary hash lookups work?

前端 未结 5 2323
轮回少年
轮回少年 2020-12-13 14:42

How do Python dictionary lookup algorithms work internally?

mydi[\'foo\'] 

If the dictionary has 1,000,000 terms, is a tree search execut

5条回答
  •  没有蜡笔的小新
    2020-12-13 15:05

    Hash lookups don't use trees. They use a hash table, and they take constant time lookup. They will take more space (on average I believe twice as much) as a tree, but the lookup and insert times are win.

    To oversimplify, take an md5 of your key, and mod that with the number of addresses you have, and that's where you save or look to retrieve a key. It doesn't matter how big the set is, it will always take the same amount of time as long as you don't have significant collision, which a good hash will avoid.

提交回复
热议问题