Python:如何将两个平面列表组合成一个二维数组?

时间:2017-01-04 15:58:22

标签: python arrays

我有两个地理坐标列表(lat,long),我需要将它们组合成一个二维数组或矩阵。

它们现在存储在数据框中:

    lat         lon
0   48.010258   -6.156909
1   48.021648   -6.105887
2   48.033028   -6.054801
3   48.044384   -6.003691
4   48.055706   -5.952602
5   48.067017   -5.901447
6   48.078304   -5.850270
7   48.089558   -5.799114
8   48.100800   -5.747891

如何将这两个列表组合成一个二维数组,以便保留 lat-lon 对应关系?这些是简单的数据:

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

修改

这些摘录数据代表(纬度,长度)(y,x)地理地图。结合起来,它们重现下面的图像。您清楚地看到存在的预期结果将被剥夺一定宽度的外部数据框架。因此,它就像切出图片的外框,其宽度为30个数据点。

5 个答案:

答案 0 :(得分:17)

list(zip(lat, long))

给出

[(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)]

有关zip here

的更多信息

答案 1 :(得分:1)

尝试使用numpy模块,即:np.column_stack 也许可以试验它,看看它是否能提供所需的结果/格式

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

结帐numpy.column_stack 希望这会有所帮助:)

答案 2 :(得分:1)

lat=[48.01,48.02,48.03,48.04,48.05,48.06,48.07,48.08,48.10]
lon=[-6.15,-6.10,-6.05,-6.00,-5.95,-5.90,-5.85,-5.79,-5.74]
mat = [[0]*2 for i in range(len(lat))]
k=0
for i, j in zip(lat, lon):
    mat[k][0]=i
    mat[k][1]=j
    k+=1
print (mat) 

答案 3 :(得分:1)

添加到Patrick Haugh的答案中:

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

会给:

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]])

答案 4 :(得分:0)

您可以将它们显式添加到新列表中并将其指定为......

coordinates = [lat, lon]

然后将coordinates设为等于......

[
 [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]
]