将UUID作为整数存储在numpy数组中

4

我需要在numpy数组中以整数格式存储大量UUID。将UUID转换为整数格式(128位)是无效的,因为可以存储在numpy数组中的最大整数大小为64位。因此,我正在尝试使用“fields”样式将UUID存储为6个单独的整数。

但是,我无法从numpy数组值中重新创建UUID。以下是问题的示例。

import uuid

#generate a random uuid4

my_uuid = uuid.uuid4()
my_uuid
# UUID('bf6cc180-52e1-42fe-b3fb-b47d238ed7ce')

# encode the uuid to 6 integers with fields
my_uuid_fields = my_uuid.fields
my_uuid_fields
# (3211575680, 21217, 17150, 179, 251, 198449560475598)

# recreate the uuid, this works
uuid.UUID(fields=my_uuid_fields)
# UUID('bf6cc180-52e1-42fe-b3fb-b47d238ed7ce')

# create an array with the fields 
my_uuid_fields_arr = np.array(my_uuid_fields)
my_uuid_fields_arr
# array([3211575680,21217,17150,179,251,198449560475598])

# convert array back to tuple and check if its the same as the initial fields tuple
assert tuple(my_uuid_fields_arr) == my_uuid_fields

# this fails however
uuid.UUID(fields = tuple(my_uuid_fields_arr))

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/scratch/6084717/ipykernel_43199/1198120499.py in <module>
----> 1 uuid.UUID(fields = tuple(my_uuid_fields_arr))

/hpc/compgen/users/mpages/software/miniconda3/envs/babe/lib/python3.7/uuid.py in __init__(self, hex, bytes, bytes_le, fields, int, version, is_safe)
    192         if int is not None:
    193             if not 0 <= int < 1<<128:
--> 194                 raise ValueError('int is out of range (need a 128-bit value)')
    195         if version is not None:
    196             if not 1 <= version <= 5:

ValueError: int is out of range (need a 128-bit value)

有没有解决这个问题的想法?

1个回答

2
tuple(my_uuid_fields_arr)np.int64类型元素的元组,而my_uuid_fieldsint类型元素的元组。显然,uuid无法正确处理numpy整数。

只需将numpy中的整数转换为Python整数即可。

uuid.UUID(fields = tuple([int(i) for i in my_uuid_fields_arr]))

当您检查元组中第一个项目的type时,您可以验证这是否是问题。


2
实际问题在于uuid.UUIDintnumpy.int64执行的位移操作。 int(3211575680)<< 96计算结果为254447239901898999706735475910225428480,而np.int64(3211575680)<< 96计算结果为0这个是一个很好的解释。 - Axe319
1
非常感谢!刚刚发现uuid.UUID(fields = my_uuid_fields_arr.tolist())也可以工作,我猜这种方法会自动转换为int - Marc P

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接