任意数组作为另一个数组的键

2024-09-30 06:32:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要一个数据结构,它可以让我将两个numpy数组与一个float关联起来。这实际上是一个二维数组,但我想使用任意数组作为键。例如:

    | a01 | a02 |
-----------------
a12 | 1.  | 3.  |
a11 | 2.  | 4.  |

其中axxnumpy数组,其中包含浮点数,例如array([ 1., 1., 2., 1., 2.])

我尝试使用numpy数组作为数据结构,但这不起作用:

>>> a = np.array([1., 2.])
>>> b = np.array([3., 4.])
>>> c = np.array([])
>>> c[a][b] = 1.
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: arrays used as indices must be of integer (or boolean) type

我不能使用python字典,因为numpy数组不能序列化

对如何存储这些数据有什么建议吗


Tags: numpy数据结构mostnp数组floatarray浮点数

热门问题