解开元组/数组/列表的包装作为Numpy数组的索引 [英] Unpacking tuples/arrays/lists as indices for Numpy Arrays

查看:98
本文介绍了解开元组/数组/列表的包装作为Numpy数组的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够做到

>>> A = numpy.array(((1,2),(3,4)))
>>> idx = (0,0)
>>> A[*idx]

并获得

1

但是这不是有效的语法.有没有没有明确写出来的方法

however this is not valid syntax. Is there a way of doing this without explicitly writing out

>>> A[idx[0], idx[1]]

?

感谢您的答复.在我的程序中,我使用Numpy数组而不是元组建立索引,并且得到了奇怪的结果.按照Alok的建议将其转换为元组即可.

Thanks for the replies. In my program I was indexing with a Numpy array rather than a tuple and getting strange results. Converting to a tuple as Alok suggests does the trick.

推荐答案

比您想象的要容易:

>>> import numpy
>>> A = numpy.array(((1,2),(3,4)))
>>> idx = (0,0)
>>> A[idx]
1

这篇关于解开元组/数组/列表的包装作为Numpy数组的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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