简体   繁体   English

从索引列表访问多维 np.array 的元素

[英]Access elements of a multi-dimension np.array from a list of indices

How can I access to the elements of a multi-dimension np.array given a list of indices?给定索引列表,如何访问多维 np.array 的元素? for example, I have a 2D np.array like arr= np.array([[10,20,30],[40,50,60]]) and a list of indices like list_ind = [(0,1), (1,1)] .例如,我有一个像arr= np.array([[10,20,30],[40,50,60]])这样的二维 np.array 和一个像list_ind = [(0,1), (1,1)] The question is that how can I access to arr(list_ind), which is arr[0,1]=20 and arr[1,1]=50 ?问题是我如何访问 arr(list_ind),即arr[0,1]=20arr[1,1]=50 Any idea?任何想法?

simply:简单地:

arr[list_ind]

or yet better:或者更好:

arr[tuple(list_ind)]

output:输出:

array([20, 50])

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

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