如何将 python int 转换为 numpy.int64?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46699280/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 17:45:50  来源:igfitidea点击:

How to convert python int into numpy.int64?

pythonnumpyint

提问by ShanZhengYang

Given a variable in python of type int, e.g.

给定一个 python 类型的变量int,例如

z = 50
type(z) 
## outputs <class 'int'>

is there a straightforward way to convert this variable into numpy.int64?

有没有一种直接的方法可以将此变量转换为numpy.int64

It appears one would have to convert this variable into a numpy array, and then convert this into int64. That feels quite convoluted.

似乎必须将此变量转换为 numpy 数组,然后将其转换为 int64。感觉挺纠结的。

https://docs.scipy.org/doc/numpy-1.13.0/user/basics.types.html

https://docs.scipy.org/doc/numpy-1.13.0/user/basics.types.html

回答by user2357112 supports Monica

numpy.int64(z)

It's that simple. Make sure you have a good reason, though - there area few good reasons to do this, but most of the time, you can just use a regular intdirectly.

就这么简单。不过,请确保您有充分的理由 - 这样有几个充分的理由,但大多数情况下,您可以直接使用常规int

回答by sascha

import numpy as np
z = 3
z = np.dtype('int64').type(z)
print(type(z))

outputs:

输出:

<class 'numpy.int64'>

But i support Juliens question in his comment.

但我在他的评论中支持 Juliens 的问题。