Python: How to get values of an array at certain index positions?

后端 未结 5 1026
执念已碎
执念已碎 2020-12-06 05:04

I have a numpy array like this:

a = [0,88,26,3,48,85,65,16,97,83,91]

How can I get the values at certain index positions in ONE step? For e

5条回答
  •  一个人的身影
    2020-12-06 05:11

    You can use index arrays, simply pass your ind_pos as an index argument as below:

    a = np.array([0,88,26,3,48,85,65,16,97,83,91])
    ind_pos = np.array([1,5,7])
    
    print(a[ind_pos])
    # [88,85,16]
    

    Index arrays do not necessarily have to be numpy arrays, they can be also be lists or any sequence-like object (though not tuples).

提交回复
热议问题