user1658296
user1658296

Reputation: 1418

Numpy: extract data and coordinates from 2-D arrays

Given:

M = [[1, 2],
     [3, 4]]

N = [[(0,1), (1, 0)],
     [(1,1), (0,0)]]

With M and N, both of dimension 2x2. How might I get a list L, of tuples, where each tuple is the combination of the position of the value, and the values from M and N:

L = [((0,0), (0,1), 1) ...]

Where the first entry above in L, is the position (0,0) in M and N, and the tuple/value (0,1) from N, and the value 1 from M.

Do I need to stack M, N to create a third dimension? And does numpy have a clean and efficient way to produce such a list of tuples?

Upvotes: 0

Views: 822

Answers (1)

Kennet Celeste
Kennet Celeste

Reputation: 4771

use this:

zip(*[iter(([((row,col),N[row][col],M[row][col]) for row in range(2) for col in range(2)]))]*2)

result:

[(((0, 0), (0, 1), 1), ((0, 1), (1, 0), 2)),
 (((1, 0), (1, 1), 3), ((1, 1), (0, 0), 4))]

Upvotes: 1

Related Questions