Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中实现哈希函数_Python_Arrays_Hash - Fatal编程技术网

在Python中实现哈希函数

在Python中实现哈希函数,python,arrays,hash,Python,Arrays,Hash,我有一组100000个ID需要散列到一个有50个桶的数组中 ID的格式为:AA00000…AA99999。我知道有类似md5的函数可用,但这些函数生成摘要,而不是数组中的索引。我如何实现一个散列,使每个ID返回一个索引到我的数组中 提前谢谢。(在Python中实现)只需使用内置函数,并使用模数将结果限制为50: hash(yourid) % 50 从文件中: 返回对象的哈希值(如果有)。散列值是整数 对于给定的输入,这将足够均匀地拾取插槽: 为什么不reduce(operator.xor,ma

我有一组100000个ID需要散列到一个有50个桶的数组中

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

提前谢谢。(在Python中实现)

只需使用内置函数,并使用模数将结果限制为50:

hash(yourid) % 50
从文件中:

返回对象的哈希值(如果有)。散列值是整数

对于给定的输入,这将足够均匀地拾取插槽:


为什么不
reduce(operator.xor,map(ord,my_string))%50
:P(+1)谢谢Martijn;hash()函数是否生成均匀分布?@JoranBeasley:我想知道的是,为什么要养一只狗,然后自己吠叫……我现在正在运行它,但它没有给我一个均匀分布(甚至接近)。把每个数字乘以7或31,然后求和,然后用模来限制,怎么样?@SamTurani:啊,看,你没问这个问题。但我相信它确实如此;我为你添加了一个直方图。
>>> 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: *************************************************