Python – How do I flatten such an array using Python?

How do I flatten such an array using Python?… here is a solution to the problem.

How do I flatten such an array using Python?

I have this array here :

a = np.array([[['1','2','3'],['10','11','12']],[['4','5','6'],['13','14','15']],[['7','8','9'],['16','17','18']]])

I want to convert it like this :

>>>[['1' '2' '3'] ['4' '5' '6'] ['7' '8' '9'] ['13' '14' '15'] ['10' '11' 12'] ['16' '17' '18']]

I’m using .flatten() for numpy, but it doesn’t work

Thanks

Solution

Try:

a.transpose(1, 0, 2).reshape(-1, 3)

Related Problems and Solutions