是否可以将numpy int64数组转换为int类型

时间:2018-08-01 17:47:16

标签: python arrays numpy int

我正在尝试将64位整数的numpy数组转换为标准python整数(即类型为int的变量)的数组。

在我幼稚的想法中,我相信np.int64代表64位整数,而int代表标准python整数,但这似乎并不正确:

import numpy as np

a = np.arange(2)
print(a.dtype)  # dtype('int64')
print(type(a[0]))  # <class 'numpy.int64'>
b = a.astype(int)
print(b.dtype)  # dtype('int64')
print(type(b[0]))  # <class 'numpy.int64'>
c = a.astype('int')
print(c.dtype)  # dtype('int64')
print(type(c[0]))  # <class 'numpy.int64'>

当然有用的一件事是:

d = a.tolist()
print(type(d[0]))  # int

是否可能有一个数字类型为int的numpy数组,或者numpy是否要求变量具有与其等效的np.int数据类型?

1 个答案:

答案 0 :(得分:0)

这只是评论的转贴,以结束问题。

  

内置int不是有效的dtype。它将转换为   np.int_是什么,可能是np.int64np.int32,具体取决于   在您的平台上。可以,但是必须使用dtype = object,   本质上消除了numpy的优势,从而为您提供了Python列表。

通过 juanpa.arrivillaga

相关问题