简体   繁体   中英

numpy array using python's long type

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 ; ie, they are not Python long s.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM