在 python 中初始化 64 x 64 个 numpy 的 (0,0) 元组?

Initialize 64 by 64 numpy of (0,0) tuples in python?

是否可以创建任意数据结构的 numpy,例如元组?如果是,我如何在不写出来的情况下初始化它? (显然,我不想写出 64 x 64 数组)

创建一个空数组 dtype=object:

a=np.empty((64,64), dtype=object)

然后将元组(或其他任何东西)放入其中:

for y in range(64):
    for x in range(64):
        a[y,x] = (0,0)

实际上最重要的是 dtype=object,允许您将任何 Python 对象放入其中(但是会损失向量化操作的速度)。

另一种方式:

value = np.empty((), dtype=object)
value[()] = (0, 0)
a = np.full((64, 64), value, dtype=object)

这里需要一些技巧来确保 numpy 不会尝试迭代元组,因此在对象数组中进行初始包装