Python: how to combine two flat lists into a 2D array?

You can just explicitly add them to a new list and assign it like...

coordinates = [lat, lon]

It would then set coordinates equal to...

[
 [48.01,48.02,48.03,48.04,48.05,48.06,48.07,48.08,48.10],
 [-6.15,-6.10,-6.05,-6.00,-5.95,-5.90,-5.85,-5.79,-5.74]
]

Try using the numpy module i.e: np.column_stack maybe experiment with it to see if it gives you the desired result/format

>>> np.column_stack((lat, lon))

check out numpy.column_stack hope this helps :)


Adding to Patrick Haugh's answer:

np.array(list(zip(lat, lon)))

Will give:

array([[48.01, -6.15],
       [48.02, -6.1 ],
       [48.03, -6.05],
       [48.04, -6.  ],
       [48.05, -5.95],
       [48.06, -5.9 ],
       [48.07, -5.85],
       [48.08, -5.79],
       [48.1 , -5.74]])

list(zip(lat, long))

gives

[(48.01, -6.15), (48.02, -6.1), (48.03, -6.05), (48.04, -6.0), 
 (48.05, -5.95), (48.06, -5.9), (48.07, -5.85), (48.08, -5.79), (48.1, -5.74)]

More on zip here

Tags:

Python

Arrays