使用python的long类型的numpy数组 [英] numpy array using python's long type
问题描述
我正在尝试创建类型为long
的numpy数组(即Python的long
,而不是numpy的long = int64
).
I am trying to create a numpy array with type long
(that is, Python's long
, not numpy's long = int64
).
如果我这样做:
m = np.eye(6, dtype=long)
print(m.dtype)
这将输出int64
;也就是说,它们不是Python long
.
This outputs int64
; i.e., they are not Python long
s.
有没有办法创建一个每个元素的类型都为long
的numpy数组?还是这是numpy不支持的某些固定宽度与非固定宽度问题?如果是这样,是否有任何库(最好具有类似numpy的C语言API)可以做到这一点?
Is there any way to create a numpy array with each element being of type long
? Or is this some fixed-width versus non-fixed-width problem that numpy doesn't support? If so, is there any library (preferably with a nice C API like numpy's) that can do this?
推荐答案
Python的长整数类型不是本机numpy类型,因此您必须
使用object
数据类型. object
类型的numpy数组的元素可以是任何python对象.
Python's long integer type is not a native numpy type, so you will have to
use the object
data type. The elements of an numpy array with object
type can be any python objects.
例如,
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
这对您是否有用取决于您要对多头数组进行的操作.
Whether or not this is useful for you depends on what you want to do with the array of longs.
这篇关于使用python的long类型的numpy数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!