在Python中实现hash函数

2024-09-23 22:24:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一组100000个id需要散列到一个有50个bucket的数组中。你知道吗

ID的格式为:AA00000…AA99999。我知道有类似md5的函数可用,但这些函数生成摘要而不是数组中的索引。我如何实现一个散列,使它为每个ID返回一个索引到我的数组中?你知道吗

提前谢谢。(用Python实现)


Tags: 函数idbucket格式数组md5aa99999aa00000
1条回答
网友
1楼 · 发布于 2024-09-23 22:24:17

只需使用内置的^{} function,并使用模数将结果限制为50:

hash(yourid) % 50

根据文件:

Return the hash value of the object (if it has one). Hash values are integers.

对于给定的输入,这会选择足够的插槽:

>>> from collections import Counter
>>> histogram = Counter(hash('AA{:05d}'.format(i)) % 50 for i in range(100000))
>>> for i in range(50):
...     print '{:4d}: {}'.format(histogram[i], '*' * (histogram[i] // 40))
... 
1932: ************************************************
1932: ************************************************
1941: ************************************************
1941: ************************************************
1908: ***********************************************
1908: ***********************************************
1974: *************************************************
1974: *************************************************
2012: **************************************************
2012: **************************************************
1898: ***********************************************
1898: ***********************************************
1954: ************************************************
1954: ************************************************
1925: ************************************************
1925: ************************************************
1995: *************************************************
1995: *************************************************
1982: *************************************************
1982: *************************************************
2023: **************************************************
2023: **************************************************
2025: **************************************************
2025: **************************************************
2070: ***************************************************
2070: ***************************************************
2042: ***************************************************
2042: ***************************************************
2028: **************************************************
2028: **************************************************
2120: *****************************************************
2120: *****************************************************
2064: ***************************************************
2064: ***************************************************
2100: ****************************************************
2100: ****************************************************
2057: ***************************************************
2057: ***************************************************
2039: **************************************************
2039: **************************************************
1981: *************************************************
1981: *************************************************
1956: ************************************************
1956: ************************************************
2000: **************************************************
2000: **************************************************
1982: *************************************************
1982: *************************************************
1992: *************************************************
1992: *************************************************

相关问题 更多 >