long类型python,使用python的long类型的numpy数组

I am trying to create a numpy array with type long (that is, Python's long, not numpy's long = int64).

If I do:

m = np.eye(6, dtype=long)

print(m.dtype)

This outputs int64; i.e., they are not Python longs.

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'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.

For example,

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值