简体   繁体   English

为什么numpy数组将int转换为float

[英]Why do numpy array turns int into float

I'm trying to fill an array with integers, but it seems like numpy array keep turning the integers into floats. 我正在尝试用整数填充数组,但似乎numpy array不断将整数转换为浮点数。 Why is this happening and how do I stop this? 为什么会发生这种情况,我该如何停止呢?

arr = np.empty(9)
arr[3] = 7
print(arr[3])
>>>7.0

NumPy arrays, unlike Python lists, can contain only a single type, which (as far as I know) is set at creation time. NumPy数组与Python列表不同,只能包含一个类型(据我所知)是在创建时设置的。 Everything you put into the array gets converted to that type. 您放入数组中的所有内容都将转换为该类型。

By default, the data type is assumed to be float . 默认情况下,假定数据类型为float To set another type, you can pass dtype to the empty function like this: 要设置其他类型,可以将dtype传递给empty函数,如下所示:

>>> arr = np.empty(9, dtype=int)
>>> arr[3] = 7
>>> arr[3]
7

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

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