关于return hash(key) % len(aMap)

muench 发布于 2015年10月13日 | 更新于 2016年06月15日
无人欣赏。

本人正在学习learn python the hard way -- ex40 当中有一串代码是这样的

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)

在网上查了一下hash,但是还是不能很理解。 所以想请教hash的究竟是用来做什么的,它的目的是什么,在哪里可以用到。

其次为什么在这里使用%百分号?

共5条回复
watcher 回复于 2015年10月13日

百分号? 哥哥,你翻页学的吧...

muench 回复于 2015年10月13日

1楼 @watcher len(xx)是算xx里面有多少值的, 是一定要配套使用%号吗?

mutoulbj 回复于 2015年10月14日

2楼 @muench 这是取余操作

>>> 10 % 3

1

muench 回复于 2015年10月15日

3楼 @mutoulbj 真的非常感谢 :)

TATOAO 回复于 2016年06月15日

4楼 @muench 你学会了吗?刚好看到这一章,这里为什么要取余呢?

登录 或者 注册
相关帖子