2017-02-01 60 views
0

這看起來非常簡單。在某種程度上,就好像我只需要擺脫一些方括號。我想使用一個數組作爲多維數組的索引。我一定是個假人。這是我最小的例子:Numpy數組作爲多維numpy數組中的索引

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]]) 

謝謝!

回答

0

只是將您的索引數組轉換爲元組

+0

這並沒有提供問題的答案。要批評或要求作者澄清,請在其帖子下方留言。 - [來自評論](/ review/low-quality-posts/15065236) – Lundin

+0

@Lundin不是嗎?好吧,有一個令人困惑的錯字('列表',而不是'數組'),但除此之外呢?其實,爲什麼不看看[接受的答案](http://stackoverflow.com/a/41951183/7207392)重複? –

+0

此答案的問題與鏈接副本中接受的問題不同,您不提供解釋爲什麼或如何解決問題的解釋。以目前的形式來看,這可能是更好的評論,而不是一個答案。 – Lundin