Python 如何调用numpy数组中的元素?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3582601/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 11:49:05  来源:igfitidea点击:

How to call an element in a numpy array?

pythonarraysnumpy

提问by kame

This is a really simple question, but I didnt find the answer. How to call an element in an numpy array?

这是一个非常简单的问题,但我没有找到答案。如何调用numpy数组中的元素?

import numpy as np

arr = np.array([[1,2,3,4,5],[6,7,8,9,10]])

print arr(0,0)

The code above doesn't work.

上面的代码不起作用。

采纳答案by carl

Just use square brackets instead:

只需使用方括号:

print arr[1,1]

回答by alvas

TL;DR:

特尔;博士

Using slicing:

使用切片

>>> import numpy as np
>>> 
>>> arr = np.array([[1,2,3,4,5],[6,7,8,9,10]])
>>> 
>>> arr[0,0]
1
>>> arr[1,1]
7
>>> arr[1,0]
6
>>> arr[1,-1]
10
>>> arr[1,-2]
9


In Long:

在长:

Hopefully this helps in your understanding:

希望这有助于您的理解:

>>> import numpy as np
>>> np.array([ [1,2,3], [4,5,6] ])
array([[1, 2, 3],
       [4, 5, 6]])
>>> x = np.array([ [1,2,3], [4,5,6] ])
>>> x[1][2] # 2nd row, 3rd column 
6
>>> x[1,2] # Similarly
6

But to appreciate why slicingis useful, in more dimensions:

但是要了解为什么切片是有用的,在更多方面:

>>> np.array([ [[1,2,3], [4,5,6]], [[7,8,9],[10,11,12]] ])
array([[[ 1,  2,  3],
        [ 4,  5,  6]],

       [[ 7,  8,  9],
        [10, 11, 12]]])
>>> x = np.array([ [[1,2,3], [4,5,6]], [[7,8,9],[10,11,12]] ])

>>> x[1][0][2] # 2nd matrix, 1st row, 3rd column
9
>>> x[1,0,2] # Similarly
9

>>> x[1][0:2][2] # 2nd matrix, 1st row, 3rd column
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: index 2 is out of bounds for axis 0 with size 2

>>> x[1, 0:2, 2] # 2nd matrix, 1st and 2nd row, 3rd column
array([ 9, 12])

>>> x[1, 0:2, 1:3] # 2nd matrix, 1st and 2nd row, 2nd and 3rd column
array([[ 8,  9],
       [11, 12]])

回答by imflash217

If you are using numpyand your array is an np.arrayof np.arrayelements like:

如果使用的是numpy和你的阵列是np.arraynp.array相同的元素:

A = np.array([np.array([10,11,12,13]), np.array([15,16,17,18]), np.array([19,110,111,112])])

A = np.array([np.array([10,11,12,13]), np.array([15,16,17,18]), np.array([19,110,111,112])])

and you want to access the inner elements (like 10,11,12 13,14.......) then use:

并且您想访问内部元素(如10,11,12 13,14.......)然后使用:

A[0][0]instead of A[0,0]

A[0][0]代替 A[0,0]

For example:

例如:

>>> import numpy as np
>>>A = np.array([np.array([10,11,12,13]), np.array([15,16,17,18]), np.array([19,110,111,112])])
>>> A[0][0]
>>> 10
>>> A[0,0]
>>> Throws ERROR

(P.S.: Might be useful when using numpy.array_split())

(PS:使用时可能有用numpy.array_split()

回答by u10405951

Also, you could try to use ndarray.item(), for example, arr.item((0, 0))(rowid+colid to index) or arr.item(0)(flatten index), its doc https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.item.html

此外,您可以尝试使用ndarray.item()例如arr.item((0, 0))(rowid+colid to index) 或arr.item(0)(flatten index),其文档https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.item。 html