Numpy数组作为多维numpy数组中的索引

时间:2017-02-01 05:55:03

标签: python arrays numpy multidimensional-array

这似乎很简单。在某种程度上,它就好像我只需要摆脱一些方括号。我想使用数组作为多维数组的索引。我一定是个假人。这是我最小的例子:

import numpy as np

## I know this is a dumb way to initialize.
state = np.array([[[1,2,3],[4,5,6],[7,8,9]],[[10,11,12],[13,14,15],[16,17,18]]])
print('state\n',state)
a = np.array([0,0,0])
print('a',a)

## I want to use a as the indices of state a la:
print('state[a]\n',state[a])

## If I could just get rid of the brackets I'd be fine:
print('state[0,0,0]\n',state[0,0,0])

## This gets me what I want, but it is UGLY in non-minimal case:
print('state[a[0],a[1],a[2]]\n',state[a[0],a[1],a[2]])

谢谢!

1 个答案:

答案 0 :(得分:0)

只需将索引数组转换为元组