numpy `astype(int)` 给出 `np.int64` 而不是 `int` - 该怎么办?

问题描述 投票:0回答:1

我有一个标准

np.array
,类型为
np.int32

idx = array([ 607,  638,  639, ..., 9279, 9317, 9318], dtype=int32)

为了确保

int32
实际上是
np.int32
,我检查:

>>> type(idx[0])
<class 'numpy.int32'>

为了稍后使用这个数组,我需要它们的类型为

int
(原因是我想使用
std::vector<T>
来索引a
cppyy
,这似乎对索引类型很严格)。因此我想我可以做

>>> idx2 = idx.astype(int)

但这给出了

>>> type(idx2[0])
<class 'numpy.int64'>

我必须使用这样的东西吗?

>>> idx3 = [int(k) for k in idx]
>>> type(idx3[0])
<class 'int'>

有什么建议吗?

python numpy type-conversion
1个回答
0
投票

IIUC,你可以使用

.tolist()
:

idx = np.array([607, 638, 939], dtype=np.int32)
idx3 = idx.tolist()

print(type(idx3[0]))

打印:

<class 'int'>
© www.soinside.com 2019 - 2024. All rights reserved.