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

标签 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 数据类型?

最佳答案

这只是为了结束问题而重新发布的评论。

The built-in int isn't a valid dtype. It will be converted to whatever np.int_ is, which may be np.int64 or np.int32 depending on your platform. You can, but you must use dtype=object, which essentially removes the advantages of numpy to give you a Python list.

作者:juanpa.arrivilillaga

关于python - 是否可以将 numpy int64 数组转换为 int 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51639373/

相关文章:

python - 如何在登录表单上引发 django 验证错误(位于 index.html 的引导模式上)?

python - 如何从一行文本创建字典?

python - 使用 Python 和哈希表查找链表末尾的第 n 个节点

python - OpenCV:表示 BGR 图像的数组的维度是多少?

javascript - 我如何循环访问这种 javascript 对象?

c++ - 通过指针将 char 数组传输到另一个临时数组指针

arrays - Bash循环遍历数组

python - 在 ctypes 结构中访问 np.array

python - setup.py 找不到 numpy header

python - numpy.search在多维数组中排序