Python hash编码(go hash编码)

id="中国人"

首先,go语言hash:

Go 复制代码
import (mmh3 "murmurhash3")
mmh3.Murmurhash3([]byte(id))

对应到Python hash编码,可以直接使用mmh3

python 复制代码
import mmh3
mmh3.hash(id,signed=False)

其源码可以表示为

python 复制代码
def sum32WithSeed(datas, seed=0):
    c1_32 = 0xcc9e2d51
    c2_32 = 0x1b873593
    h1 = seed
    datas_bytes = datas.encode('utf-8')
    datas_bytes_len = len(datas_bytes)
    if datas_bytes_len == 0:
        return 0
    nblocks = datas_bytes_len // 4
    for id in range(datas_bytes_len):
        if id % 4 != 0 or id + 4 > datas_bytes_len:
            continue
        k1 = int.from_bytes(datas_bytes[id:id + 4], byteorder='little', signed=False)
        k1 *= c1_32
        k1 &= 0xffffffff
        k1 = (k1 << 15) | (k1 >> 17)
        k1 *= c2_32
        k1 &= 0xffffffff
        h1 ^= k1
        h1 = (h1 << 13) | (h1 >> 19)
        h1 = h1 * 4 + h1 + 0xe6546b64
        h1 &= 0xffffffff
    tail = datas_bytes[nblocks * 4:]
    tail_len = len(tail)
    k1 = 0
    for id in [3, 2, 1]:
        if tail_len >= id and id == 3:
            k1 ^= int.from_bytes(tail[2:3], byteorder='little', signed=False) << 16
        if tail_len >= id and id == 2:
            k1 ^= int.from_bytes(tail[1:2], byteorder='little', signed=False) << 8
        if tail_len >= id and id == 1:
            k1 ^= int.from_bytes(tail[0:1], byteorder='little', signed=False)
            k1 *= c1_32
            k1 &= 0xffffffff
            k1 = (k1 << 15) | (k1 >> 17)
            k1 *= c2_32
            k1 &= 0xffffffff
            h1 ^= k1
            h1 &= 0xffffffff

    h1 ^= datas_bytes_len
    h1 ^= h1 >> 16
    h1 *= 0x85ebca6b
    h1 &= 0xffffffff
    h1 ^= h1 >> 13
    h1 *= 0xc2b2ae35
    h1 &= 0xffffffff
    h1 ^= h1 >> 16
    return h1


def sum32(datas):
    return sum32WithSeed(datas, 0)


print(sum32(id))
相关推荐
xingshanchang42 分钟前
PyTorch 不支持旧GPU的异常状态与解决方案:CUDNN_STATUS_NOT_SUPPORTED_ARCH_MISMATCH
人工智能·pytorch·python
hn小菜鸡2 小时前
LeetCode 377.组合总和IV
数据结构·算法·leetcode
Deepoch3 小时前
Deepoc 大模型:无人机行业的智能变革引擎
人工智能·科技·算法·ai·动态规划·无人机
费弗里4 小时前
Python全栈应用开发利器Dash 3.x新版本介绍(1)
python·dash
李少兄9 天前
解决OSS存储桶未创建导致的XML错误
xml·开发语言·python
就叫飞六吧9 天前
基于keepalived、vip实现高可用nginx (centos)
python·nginx·centos
Vertira9 天前
PyTorch中的permute, transpose, view, reshape和flatten函数详解(已解决)
人工智能·pytorch·python
heimeiyingwang9 天前
【深度学习加速探秘】Winograd 卷积算法:让计算效率 “飞” 起来
人工智能·深度学习·算法
学Linux的语莫9 天前
python基础语法
开发语言·python
匿名的魔术师9 天前
实验问题记录:PyTorch Tensor 也会出现 a = b 赋值后,修改 a 会影响 b 的情况
人工智能·pytorch·python