2017-06-14 49 views
0

參考我以前的(已解決)問題(link),我現在想要在多維數組上執行該操作。遍歷數組並獲得多個索引的維度

vertices = [[ 1.25, 4.321, -4], [2, -5, 3.32], [23.3, 43, 12], [32, 4, -23]] 

newedges = [[1, 3, 2, 0], [2, 1, 3, 0], [1, 2, 0, 3]] 

newresult = [[[2, -5, 3.32], [32, 4, -23], [23.3, 43, 12], [ 1.25, 4.321, -4]], [[23.3, 43, 12], [2, -5, 3.32], [32, 4, -23], [ 1.25, 4.321, -4]], [[2, -5, 3.32], [23.3, 43, 12], [ 1.25, 4.321, -4], [32, 4, -23]]] 

我想回去用同樣形狀的排列爲「newedges」,但與頂點替代指標( - > newresult)。

我想:

list =() 
arr = np.ndarray(newedges.shape[0]) 

for idx, element in enumerate(newedges): 

    arr[idx] = vertices[newedges[idx]] 

list.append(arr) 

,但得到的指數誤差(與我的真實數據,這就是爲什麼有一個索引61441):

IndexError: index 61441 is out of bounds for axis 1 with size 2 
+0

'vertices'是一個多維數組,您傳遞給它的第一個軸的索引超出了範圍。 – Kasramvd

+0

上一個問題鏈接似乎不準確/中斷。它目前指向http://www.example.com/ – vishal

+0

@vishal我更正了它 –

回答

1

在這裏你去:

import numpy as np 

vertices = [[ 1.25, 4.321, -4], [2, -5, 3.32], [23.3, 43, 12], [32, 4, -23]] 
vertices= np.array(vertices) 
newedges = [[1, 3, 2, 0], [2, 1, 3, 0], [1, 2, 0, 3]] 

newresult = [] 

for edgeset in newedges: 
    updatededges = np.take(vertices, edgeset, 0) 
    newresult.append(updatededges) 

print newresult 
""" 
newresult = [array([[ 2. , -5. , 3.32 ], 
     [ 32. , 4. , -23. ], 
     [ 23.3 , 43. , 12. ], 
     [ 1.25 , 4.321, -4. ]]), 

array([[ 23.3 , 43. , 12. ], 
     [ 2. , -5. , 3.32 ], 
     [ 32. , 4. , -23. ], 
     [ 1.25 , 4.321, -4. ]]), 

array([[ 2. , -5. , 3.32 ], 
     [ 23.3 , 43. , 12. ], 
     [ 1.25 , 4.321, -4. ], 
     [ 32. , 4. , -23. ]])] 
""" 

另一個建議是千萬不要使用像list這樣的python關鍵字作爲變量名稱。這同樣適用於任何編程語言

0

在第3行,你錯過了一個]

前:

newedges = [[1, 3, 2, 0], [2, 1, 3, 0], [1, 2, 0, 3] 

之後:

newedges = [[1, 3, 2, 0], [2, 1, 3, 0], [1, 2, 0, 3]] 

如果你不這樣做,第5行被認爲是一個字符串。

那麼你有其他問題,要看到它是什麼,用that,你PROGRAMM已經內,奮力向前,並等待錯誤

+0

這是一個拼寫錯誤,謝謝 –

+0

沒有問題,祝你好運! 不要忘記關閉這個問題:) –

+0

我會,一旦它解決了 - 正確的拼寫不能解決它 –

1

,而不是這個list=()你必須使用result = []

取代:arr = np.ndarray(newedges.shape[0])

到:arr = np.ndarray(newedges[0]).shape

for idx, element in enumerate(newedges): 
    arr[idx] = vertices[newedges[0][idx]] 

result.append(arr) 

你得到IndexError,因爲你通過名單vertices[newedges[idx]]名單但列表需要索引或分片vertices[newedges[0][idx]]

希望這個答案是你想要的。