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

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

问题描述

假设我有

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

b = array([1,1])

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

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

我大概可以做到

a[tuple(b)]

有更好的方法吗?

谢谢

推荐答案

根据 NumPy 教程,正确的做法是:

According the NumPy tutorial, the correct way to do it is:

a[tuple(b)]

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

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