how can I change a two dimensional array to one dimensional

Viewed 14855

I am trying to change a two dimensional array to one dimensional, my code is like this:

x = np.array([[1, 2, 4], [3, 4], [1,2,3,4,5,6,7]])
x = x.flatten()

however, I found that flatten function works well on

x = np.array([[1, 2], [3, 4]])

but it does not work on

x = np.array([[1, 2, 4], [3, 4], [1,2,3,4,5,6,7]])

could anyone help me to change

np.array([[1, 2, 4], [3, 4], [1,2,3,4,5,6,7]]) 

to

np.array([[1, 2, 4, 3, 4, 1,2,3,4,5,6,7])

thank you

3 Answers
t = [[4, 9, 2], [3, 5, 7], [8, 1, 5]]
arr_t=[]
for a in t:
    arr_t+=a

Use the following simple code.

import numpy as np

x = np.array([[1, 2, 4], [3, 4], [1,2,3,4,5,6,7]])
y = sum(x.tolist(),[])
print(np.array(y))
Related