numpy数组使用python的long类型

use*_*937 5 python arrays numpy

我正在尝试创建一个类型为numpy的数组long(即Python long,而不是numpy long = int64).

如果我做:

m = np.eye(6, dtype=long)
print(m.dtype)
Run Code Online (Sandbox Code Playgroud)

这输出int64; 即,它们不是Python long.

有没有办法创建一个numpy数组,每个元素都是类型long?或者这是numpy不支持的固定宽度与非固定宽度问题?如果是这样,是否有任何库(最好有一个很好的C API,如numpy的)可以做到这一点?

War*_*ser 7

Python的长整数类型不是本机numpy类型,因此您必须使用object数据类型.具有object类型的numpy数组的元素可以是任何python对象.

例如,

In [1]: x = np.array([1L, 2L, 3L], dtype=object)

In [2]: x
Out[2]: array([1L, 2L, 3L], dtype=object)

In [3]: x[0]
Out[3]: 1L

In [4]: type(x[0])
Out[4]: long
Run Code Online (Sandbox Code Playgroud)

这是否对你有用取决于你想要对long数组做什么.