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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41468116/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 01:01:24  来源:igfitidea点击:

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

pythonarrays

提问by FaCoffee

I have two flat lists of geographical coordinates (lat, long), and I need to combine them into a 2D array or matrix.

我有两个平面的地理坐标列表(经纬度),我需要将它们组合成一个二维数组或矩阵。

They are now stored in a dataframe:

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

    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

How can I combine these two lists into a 2D array so that the lat-loncorrespondence is preserved? These are the plain data:

如何将这两个列表组合成一个二维数组,以便保留经纬度对应关系?这些是普通数据:

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]

EDIT

编辑

These excerpted data represent a (lat, long)or (y, x)geographical map. Combined, they reproduce the below image. You clearly see the presence of The intended outcome will have to be deprived of an outer frame of data of a certain width. So it's like cutting out an outer frame of a picture, the width of which is 30 data points.

这些摘录的数据代表(纬度,经度)(y,x)地理地图。结合起来,它们再现了下图。你清楚地看到存在 预期的结果将不得不被剥夺一定宽度的数据的外部框架。所以就像剪下一张图片的外框,宽度为30个数据点。

回答by Patrick Haugh

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 ziphere

更多在zip这里

回答by Just Ice

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

尝试使用 numpy 模块,即:np.column_stack也许可以试验它,看看它是否给你想要的结果/格式

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

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

check out numpy.column_stackhope this helps :)

查看numpy.column_stack希望这有帮助:)

回答by jophab

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) 

回答by m_callens

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

您可以明确地将它们添加到一个新列表中,并像...

coordinates = [lat, lon]

It would then set coordinatesequal to...

然后它将设置为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]
]