Python:如何在某些索引位置获取数组的值? [英] Python: How to get values of an array at certain index positions?

查看:424
本文介绍了Python:如何在某些索引位置获取数组的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的numpy数组:

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 example:

ind_pos = [1,5,7]

结果应为:

[88,85,16]


推荐答案

只需使用你的索引 ind_pos

ind_pos = [1,5,7]
print (a[ind_pos]) 
[88 85 16]


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

In [56]: import numpy as np

In [57]: arr = np.array(a)

In [58]: ind_pos = [1,5,7]

In [59]: arr[ind_pos]
Out[59]: array([88, 85, 16])

这篇关于Python:如何在某些索引位置获取数组的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆