将整数数组编码为唯一的int [英] Encode array of integers into unique int

查看:75
本文介绍了将整数数组编码为唯一的int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有固定数量的int数组,形式为:

I have a fixed amount of int arrays of the form:

[a,b,c,d,e]

例如:

[2,2,1,1,2]

其中ab可以是0到2之间的整数,cd可以是0或1,并且e可以是0到2之间的整数.

where a and b can be ints from 0 to 2, c and d can be 0 or 1, and e can be ints from 0 to 2.

因此有:3 * 3 * 2 * 2 * 3:108这种形式的可能数组.

Therefore there are: 3 * 3 * 2 * 2 * 3: 108 possible arrays of this form.

我想为每个数组分配一个从0到107的唯一整数代码.

I would like to assign to each of those arrays a unique integer code from 0 to 107.

我被困住了,我想将数组中的每个数字相加,但是要加上两个数组,例如:

I am stuck, i thought of adding each numbers in the array, but two arrays such as:

[0,0,0,0,1] and [1,0,0,0,0]

都将加到1.

有什么建议吗?

谢谢.

推荐答案

您可以使用

You could use np.ravel_multi_index:

>>> np.ravel_multi_index([1, 2, 0, 1, 2], (3, 3, 2, 2, 3))
65

验证:

>>> {np.ravel_multi_index(j, (3, 3, 2, 2, 3)) for j in itertools.product(*map(range, (3,3,2,2,3)))} == set(range(np.prod((3, 3, 2, 2, 3))))
True

返回另一种方式:

>>> np.unravel_index(65, dims=(3, 3, 2, 2, 3))
(1, 2, 0, 1, 2)

这篇关于将整数数组编码为唯一的int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆