学习Python是困难的Ex39,很难理解它的代码 [英] Learn Python the hard way Ex39, having a hard time understanding its code
问题描述
hash%10
来计算。这会将每个项目的散列值与想要的范围[0,10)中的一个桶匹配。 I'm having a hard time understanding a lot of the code in Learn Python The Hard Way Ex39.
My first confusion is regarding why the author used modular division here hash(key) % len(aMap)
. What value does it return? Why does author need the reminders?
def hash_key(aMap, key):
"""Given a key this will create a number and then convert it to an index for the aMap's buckets."""
return hash(key) % len(aMap)
The second confusing thing to me is the next code
def get_bucket(aMap, key):
"""Given a key, find the bucket where it would go."""
bucket_id = hash_key(aMap, key)
return aMap[bucket_id]
In my understanding, bucket_id
should be equal to the value of hash(key) % len(aMap)
so why don't we just return aMap[hash(key)]
?
Modulo is used to constrain a number to a range. This is a well known trick. That way if you overflow you "send" the number back to the range.
Suppose I want a number in [0, 9) and I have a hash generator than generates numbers in the range [0, 100). If I use hash % 10
it is guaranteed to be in my expected range. In the first piece of code the trick is used to match an item with a bucket, since hashes not necessarily limited but buckets are.
The second piece of code just fetches the bucket with the constrained hash (in bucket range). To understand it you must understand that a hash map most of the times works with buckets. A good hash map distributes items across the buckets equally (or close to it).
So if we re-iterate - if you have hashes in the range [0, 100) and 10 buckets [0, 10), the bucket index for each item could be calculated with hash % 10
. This would match each item's hash to a bucket in the wanted range [0, 10).
这篇关于学习Python是困难的Ex39,很难理解它的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!