numpy我如何在某个坐标处获取元素的值? [英] numpy how can i get the value of an element at certain coordinates?

查看:44
本文介绍了numpy我如何在某个坐标处获取元素的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经找到了获取numpy数组的最大值然后获取坐标的方法,但是有相反的方法吗?

I have found ways of getting the maximum value of a numpy array, and then getting the coordinates, but is there any way of doing the opposite?

我的意思是,通过输入2D坐标来获取数组元素的值吗?

I mean, getting the value of an array element by inputting 2D coordinates?

非常感谢您的帮助!

干杯

大卫

推荐答案

语法为 A [row_number,column_number]

第一行或第一列的编号为0.

The first row or column is numbered 0.

这里是一个例子:

In [1]: import numpy as np

In [2]: A = np.array([[1,2,3],[4,5,6],[7,8,9]])

In [3]: A
Out[3]: 
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])

In [4]: A[1,1]
Out[4]: 5

In [5]: A[2,1]
Out[5]: 8

In [6]: A[0,1]
Out[6]: 2

In [7]: A[2,2]
Out[7]: 9

您还可以使用元组作为坐标:

And you can also use tuples for coordinates:

In [8]: coords = (0,0)

In [9]: A[coords]
Out[9]: 1

In [10]: newcoords = (2,2)

In [11]: A[newcoords]
Out[11]: 9

还有其他几种读取numpy数组元素的方法.您可能希望阅读此索引文档

There are several other ways to read the elements of a numpy array. You may wish to read this Indexing documentation

这篇关于numpy我如何在某个坐标处获取元素的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆