- Python: get all possible array attributions of nd arrays. Use
itertools.product
?- If so, how?
- In Python, I have two n dimensions numpy arrays
A
andB
(B
is a zero array). - Such way
A.shape[i]<=B.shape[i]
, for any i between0
andn
. I want to create a for loop in such way every iteration I attributeA
to a different subset ofB
, in such way every possible position in occupied until the end of the for loop.
for instance, with A = np.array([[1,1,1],[1,1,1]])
and B = np.zeros((3,4))
, I would get these(one of these for each iteration):
1 1 1 0 0 1 1 1 0 0 0 0 0 0 0 0
1 1 1 0 0 1 1 1 1 1 1 0 0 1 1 1
0 0 0 0 0 0 0 0 1 1 1 0 0 1 1 1
For a fixed n
dimension it is trivial, just use nested for loops for each dimension.
However, I want it for a generic n
dimensions.
My approach was to use the itertools.product
to get all combinations of indexes.
In the above example, product([0,1],[0,1])
, would iterate over (0,0),(0,1),(1,0),(1,1)
, and I would have my indexes.
However, I don't know how to pass the values of the parameters to product function for a generic n
.
Any idea? There are better ways of doing so?