【发布时间】:2014-11-12 14:59:24
【问题描述】:
我有一个带有dtype = object 的特定数组,数组元素代表不同时间的坐标对,我想将其重塑为更简单的格式。
我设法做到了“一次”,但我无法让它在所有时间观察中都起作用。
每个观察的长度是不同的,所以也许我必须使用掩码值来做到这一点。 下面是一个例子,我希望能更好地解释我想要什么。
# My "input" is:
a = np.array([[], [(2, 0), (2, 2)], [(2, 2), (2, 0), (2, 1), (2, 2)]], dtype=object)
#And my "output" is:
#holding_array_VBPnegl
array([[2, 0],
[2, 2],
[2, 1]])
#It doesnt consider my for loop in a.shape[0], so the expected result is :
test = np.array([[[True, True],
[True, True],
[True, True]],
[[2, 0],
[2, 2],
[True, True]]
[[2, 0],
[2, 2],
[2, 1]]])
#with "True" the masked values
我尝试使用在 StackOverflow 上找到的代码:
import numpy as np
holding_list_VBPnegl=[]
for i in range(a.shape[0]):
for x in a[i]:
if x in holding_list_VBPnegl:
pass
else:
holding_list_VBPnegl.append(x)
print holding_list_VBPnegl
holding_array_VBPnegl = np.asarray(holding_list_VBPnegl)
【问题讨论】:
-
您的
test数组和您的输入a之间没有关联。请编辑您的问题(需要在a中删除(2,2)元组之一,使其看起来类似于test,但其他元组的顺序也需要更改)。