[英]2-D numpy array with tuples: expanding into multiple arrays w/combinations of original tuples
假設我有一個NumPy數組,如下所示:
0 1 (2,3)
(4,5) 6 7
9 (10,) 11
我如何將其拆分為以下數組的列表? 條件:最終數組中沒有元組,並且每個數組都包含元組元素的唯一組合。 也就是說,原始數組中有兩個元組,因此這些元素有4種不同的組合。 下面的第一個數組具有原始數組的組合(2)和(4)。
0 1 2
4 6 7
9 10 11
0 1 2
5 6 7
9 10 11
0 1 3
4 6 7
9 10 11
0 1 3
5 6 7
9 10 11
如@unutbu所建議的,這樣的數據結構很尷尬。 但是,如果您想這樣做,那是可行的:
In [52]:
A
Out[52]:
array([[0, 1, (2, 3)],
[(4, 5), 6, 7],
[9, (10,), 11]], dtype=object)
In [53]:
import itertools
AR=A.ravel()
AT=[i for i, val in enumerate(AAR) if isinstance(val, tuple)]
for item in itertools.product(*AR[AT]):
B=AR.copy()
B[AT]=item
print B.reshape(A.shape)
[[0 1 2]
[4 6 7]
[9 10 11]]
[[0 1 2]
[5 6 7]
[9 10 11]]
[[0 1 3]
[4 6 7]
[9 10 11]]
[[0 1 3]
[5 6 7]
[9 10 11]]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.