杂音散列的python包装器(杂音散列3),一组快速而健壮的散列函数。
mmh3的Python项目详细描述
mmh3
>>>>/a>是的杂音散列的python包装器(杂音散列3),一组快速而健壮的散列函数。
mmh3 2.5.1支持python 2.7、python 3.3及更高版本。
用法
示例用法:
>>> import mmh3 >>> mmh3.hash('foo') # 32 bit signed int -156908512 >>> mmh3.hash('foo', 42) # uses 42 for its seed -1322301282 >>> mmh3.hash('foo', signed=False) # 32 bit unsigned int (since Version 2.5) 4138058784
其他功能:
>>> mmh3.hash64('foo') # two 64 bit signed ints (by using the 128-bit algorithm as its backend) (-2129773440516405919, 9128664383759220103) >>> mmh3.hash64('foo',signed =False) # two 64 bit unsigned ints (16316970633193145697, 9128664383759220103) >>> mmh3.hash128('foo', 42) # 128 bit unsigned int 215966891540331383248189432718888555506 >>> mmh3.hash128('foo', 42, signed = True) # 128 bit signed int -124315475380607080215185174712879655950 >>> mmh3.hash_bytes('foo') # 128 bit value as bytes 'aE\xf5\x01W\x86q\xe2\x87}\xba+\xe4\x87\xaf~'
hash64 、 hash128 和 hash_bytes 有第三个参数用于架构优化。对于x64使用true,对于x86使用false(默认值:true)。:
>>> mmh3.hash64('foo', 42, True) (-840311307571801102, -6739155424061121879)
版本2.5添加了来自缓冲区的hash,它不需要复制内存就可以对字节进行hash。当您散列大型内存视图(如 numpy.ndarray
< Buff行情>>>> mmh3.hash_from_buffer(numpy.random.rand(100)) -2137204694 >>> mmh3.hash_from_buffer(numpy.random.rand(100), signed = False) 3812874078
注意 hash64 返回 两个值,因为它使用128位版本的murrushash3作为后端。
版本2.4增加了对64位数据的支持。
< Buff行情>>>> import numpy as np >>> a = np.zeros(2**32, dtype=np.int8) >>> mmh3.hash_bytes(a) b'V\x8f}\xad\x8eNM\xa84\x07FU\x9c\xc4\xcc\x8e'
版本2.4还将种子的类型从带符号的32位int更改为带符号的32位int。( 带符号种子的结果值仍然与以前相同,只要它们是32位的 )
< Buff行情>>>> mmh3.hash('aaaa', -1756908916) # signed rep. for 0x9747b28c 1519878282 >>> mmh3.hash('aaaa', 2538058380) # unsigned rep. for 0x9747b28c 1519878282
小心不要让这些种子超过32位。无效值可能会出现意外结果。
< Buff行情>>>> mmh3.hash('foo', 2 ** 33) -156908512 >>> mmh3.hash('foo', 2 ** 34) -156908512