用另一个numpy数组索引numpy数组 [英] Indexing numpy array with another numpy array

查看:124
本文介绍了用另一个numpy数组索引numpy数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有

a = array([[1, 2],
           [3, 4]])

b = array([1,1])

我想在索引a中使用b,即做a [b]并得到4而不是[[3,4],[3,4]]

我可能会做

a[tuple(b)]

有更好的方法吗?

谢谢

解决方案

根据numpy教程:

a[tuple(b)] 

是执行此操作的正确方法:

http://www.scipy.org/Tentative_NumPy_Tutorial#head-3f4d28139e045> /p>

Suppose I have

a = array([[1, 2],
           [3, 4]])

and

b = array([1,1])

I'd like to use b in index a, that is to do a[b] and get 4 instead of [[3, 4], [3, 4]]

I can probably do

a[tuple(b)]

Is there a better way of doing it?

Thanks

解决方案

According the numpy tutorial:

a[tuple(b)] 

is the correct way to do this:

http://www.scipy.org/Tentative_NumPy_Tutorial#head-3f4d28139e045a442f78c5218c379af64c2c8c9e

这篇关于用另一个numpy数组索引numpy数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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