将numpy数组转换为单个整数

时间:2022-06-04 01:41:38

I am a new Python user, and am struggling with what is seemingly a very easy problem — yet I cannot seem to solve it. The problem is that I created an array from np that contains a single value.

我是一个新的Python用户,我正在努力解决看似非常简单的问题 - 但我似乎无法解决它。问题是我从np创建了一个包含单个值的数组。

In:   distance_index
Out:  (array([14], dtype=int64),)

In:   type(distance_index)
Out:  tuple

So, my question is simple...I need to grab the value of 14 from the array so that I can use it in another part of the code.

所以,我的问题很简单......我需要从数组中获取14的值,以便我可以在代码的另一部分中使用它。

1 个解决方案

#1


0  

To get any index of your array just use "array[index]". Keep in mind arrays start at 0

要获取数组的任何索引,只需使用“array [index]”。请记住,数组从0开始

print(distance_index[0])

However, as @Christian Dean points out, this question deals with an array in a tuple. So you need:

然而,正如@Christian Dean指出的那样,这个问题涉及一个元组中的数组。所以你需要:

print(distance_index[0][0])

#1


0  

To get any index of your array just use "array[index]". Keep in mind arrays start at 0

要获取数组的任何索引,只需使用“array [index]”。请记住,数组从0开始

print(distance_index[0])

However, as @Christian Dean points out, this question deals with an array in a tuple. So you need:

然而,正如@Christian Dean指出的那样,这个问题涉及一个元组中的数组。所以你需要:

print(distance_index[0][0])