Unpacking tuples/arrays/lists as indices for Numpy Arrays

后端 未结 4 1306
死守一世寂寞
死守一世寂寞 2020-12-01 12:22

I would love to be able to do

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

and get

<         


        
4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-01 13:00

    No unpacking is necessary—when you have a comma between [ and ], you are making a tuple, not passing arguments. foo[bar, baz] is equivalent to foo[(bar, baz)]. So if you have a tuple t = bar, baz you would simply say foo[t].

提交回复
热议问题