2017-06-14 47 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]]

希望这个答案是你想要的。