简体   繁体   中英

Concatenate two List in a 2D Array in Python with Numpy

So, i have 2 list that i want to concatenate with numpy. For now, i'm tring to do something like this :

LeGraphiqueMatLab = np.array([LesDatesMatLab, LeGraphique], dtype=np.float64)

But it gives me an error saying : "ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 2 dimensions. The detected shape was (2, 2) + inhomogeneous part."

Do I need to use np.array on each list first and then try to add them ?

Thanks

You can use np.concatenate like that:

a = [1, 2]
b = [5, 6]
np.concatenate((a, b))

#output
array([1, 2, 5, 6])

As Phoenix stated, you can use np.concatenate() ; however, I have a feeling that LesDatesMatLab & LeGraphique may have different shapes as the error stated "inhomogeneous".

Without seeing what LesDatesMatLab & LeGraphique are, it's hard to say but try Phoenix's answer - if error persists, use LesDatesMatLab .shape & LeGraphique .shape to check if the shapes are both consistent. If not, you may need to use np.reshape() to make them consistent.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM