-5

Here in Python, I have a list like this:

array = 
[[[0,0,1,0],[0,0,0,1],[1,0,0,0],[0,1,0,0]],
[[0,1,0],[1,0,0],[0,0,1],[0,0,1]],
[[0],[1],[0],[1]],
[[1],[1],[0],[1]]]

I want to reshape it into:

array = 
[[[0,0,1,0],[0,1,0],[0],[1]],
[[0,0,0,0],[1,0,0],[1],[1]],
[[1,0,0,0],[0,0,1],[0],[0]],
[[0,1,0,0],[0,0,1],[0],[1]]]

Any suggestions?

5Volts
  • 179
  • 3
  • 13
  • 1
    2nd row 2nd col should it be [1,0,0]? – Marcus.Aurelianus Jul 17 '18 at 03:06
  • Are you flipping it over the top left-bottom right line? – Jerrybibo Jul 17 '18 at 03:06
  • 1
    Can you demonstrate *any* effort at solving this yourself? – Scott Hunter Jul 17 '18 at 03:07
  • Is this suppose to be a transpose? – juanpa.arrivillaga Jul 17 '18 at 03:07
  • 1
    Whats the logic ? – rafaelc Jul 17 '18 at 03:08
  • 1
    It looks like you're just trying to transpose a list of lists? If so, that's just `zip`. For example, `array = list(zip(*array))`. – abarnert Jul 17 '18 at 03:10
  • Also, this is tagged `numpy`, and `arrays`, but your code shows a list of lists, not a numpy array. and you even refer to it as a list. So… where is numpy supposed to be involved here? Do you want to convert the result into a 2D array? Do you actually have a 2D array instead of a list of lists? Do you just want to use numpy as an intermediate step but go from a list of lists to a list of lists? Or…? – abarnert Jul 17 '18 at 03:12
  • Anyway, I closed this as a dup of the canonical non-numpy transposing question, which has answers showing how to use `zip` and explaining why it works and everything. If you have or want a numpy array, please let us know and we can find a better duplicate that explains numpy's `.T` property for transposing. – abarnert Jul 17 '18 at 03:14
  • Thanks for the heads up! Really appreciate it. – 5Volts Jul 17 '18 at 03:15

1 Answers1

2

IIUC, given

array = [[[0,0,1,0],[0,0,0,1],[1,0,0,0],[0,1,0,0]],
        [[0,1,0],[1,0,0],[0,0,1],[0,0,1]],
        [[0],[1],[0],[1]],
        [[1],[1],[0],[1]]]

Do

import numpy as np
>>> np.array(array).T

array([[list([0, 0, 1, 0]), list([0, 1, 0]), list([0]), list([1])],
       [list([0, 0, 0, 1]), list([1, 0, 0]), list([1]), list([1])],
       [list([1, 0, 0, 0]), list([0, 0, 1]), list([0]), list([0])],
       [list([0, 1, 0, 0]), list([0, 0, 1]), list([1]), list([1])]],
rafaelc
  • 57,686
  • 15
  • 58
  • 82