简体   繁体   English

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

[英]How to convert python int into numpy.int64?

Given a variable in python of type int , eg在 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.似乎必须将此变量转换为 numpy 数组,然后将其转换为 int64。 That feels quite convoluted.感觉挺纠结的。

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

z_as_int64 = numpy.int64(z)

It's that simple.就这么简单。 Make sure you have a good reason, though - there are a few good reasons to do this, but most of the time, you can just use a regular int directly.不过,请确保您有充分的理由 - 这样有几个充分的理由,但大多数情况下,您可以直接使用常规int

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 的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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