如何使用多个索引从NumPy数组中获取值 [英] How to get the values from a NumPy array using multiple indices
本文介绍了如何使用多个索引从NumPy数组中获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个NumPy数组,如下所示:
I have a NumPy array that looks like this:
arr = np.array([100.10, 200.42, 4.14, 89.00, 34.55, 1.12])
如何通过索引从该数组中获取多个值?
How can I get multiple values from this array by index?
例如,如何获取索引位置1、4和5的值?
For example, how can I get the values at the index positions 1, 4, and 5?
我正在尝试类似的操作,这是不正确的:
I was trying something like this, which is incorrect:
arr[1, 4, 5]
推荐答案
尝试如下:
>>> arr = np.array([100.10, 200.42, 4.14, 89.00, 34.55, 1.12])
>>> arr[[1,4,5]]
array([ 200.42, 34.55, 1.12])
对于多维数组:
>>> arr = np.arange(9).reshape(3,3)
>>> arr
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
>>> arr[[0, 1, 1], [1, 0, 2]]
array([1, 3, 5])
这篇关于如何使用多个索引从NumPy数组中获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文