我很难理解Learn Python The Hard Way Ex39中的很多代码。学习Python困难的方式Ex39,很难理解它的代码
我的第一个混淆是关于作者为什么在这里使用模块划分hash(key) % len(aMap)
。它返回什么价值?为什么作者需要提醒?
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)
第二个令人困惑的事情对我来说是下一个代码
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]
在我的理解,bucket_id
应该等于hash(key) % len(aMap)
价值,为什么我们不返回aMap[hash(key)]
?
也许使用桶理解[哈希](https://en.wikipedia.org/wiki/Hash_table)的想法可能是有用的 – pazitos10