简体   繁体   English

表达式 r[[2,3],[2,3]] 如何访问 Python 中的数组

[英]How does the expression r[[2,3],[2,3]] access an array in Python

I am new to Python and I am learning from an online course.我是 Python 的新手,正在学习在线课程。 In a quiz, I was asked this question:在一个测验中,有人问我这个问题:

The choices选择

I have correctly solved it, but I was looking at the other choices wondering how they would select indices in an array and I have understood all but the 2nd choice:我已经正确地解决了它,但我正在查看其他选择,想知道它们将如何在数组中创建 select 索引,除了第二个选择之外,我已经理解了所有的选择:

r[[2,3],[2,3]]

I tried searching for it online but I haven't found an example like it.我试着在网上搜索它,但我还没有找到类似的例子。 Is this correct Python syntax?这是正确的 Python 语法吗? And if so, how would it access an array?如果是这样,它将如何访问数组?

yes, it will return r[2,2] and r[3,3] as answer.是的,它将返回r[2,2]r[3,3]作为答案。

In this syntax, first list will correspond to the row coordinates and the second list will correspond to its respective col coordinates.在此语法中,第一个列表将对应于行坐标,第二个列表将对应于其各自的列坐标。

For eg:例如:

r = np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]])
print(r[[2,3],[2,3]])

output will be output 会

array([11, 16])

The syntax is probably incorrect.语法可能不正确。 The list index must be integer, not tuple列表索引必须是 integer,不是元组

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

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