Home > Software design >  Concatenate two List in a 2D Array in Python with Numpy
Concatenate two List in a 2D Array in Python with Numpy

Time:05-19

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

CodePudding user response:

You can use np.concatenate like that:

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

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

CodePudding user response:

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.

  • Related