0

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?

7
  • N is 3 dimensional, is this what you meant? Commented Oct 28, 2016 at 12:05
  • No, N is a 2x2 array of tuples. Commented Oct 28, 2016 at 12:06
  • Have you tried with numpy.ndenumerate ? Commented Oct 28, 2016 at 12:10
  • Yes, that makes it 3d. Try using printing np.array(N).shape to see. In any case, why do you need the position in L? I mean it seems like saving L as an array where L[0][0]=((0,1),1) would be nicer instead of saving the indices. Commented Oct 28, 2016 at 12:10
  • The elements in L just map the index to the values. That is no additional information if you already have M and N, since you can just index M and N directly without storing the mapping, so you don't need L at all. What exactly are you trying to do? Commented Oct 28, 2016 at 12:53

1 Answer 1

1

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))]
Sign up to request clarification or add additional context in comments.

3 Comments

What difference does np.array make? This just list code.
@hpaulj Well actually in my code I used list at last, but I forgot to copy it here. I just summarized my code. Thanks for mentioning
I wonder if the OP really has arrays. N looks more like a nested list that np.array would turn into a 3d array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.