合并两个列表以创建一个列表,其中包含两个列表的项目作为列表
Merge two lists to create a list which contains items of the two lists as lists
我想合并两个列表:
lon: [14.21055347, 14.21055347, 16.39356558, 16.39356558, 14.21055347]
lat: [48.22824817, 48.22824817, 48.18617251, 48.18617251, 47.65823679]
获得:
coordinates: [[14.21055347, 48.22824817], [14.21055347, 48.22824817], [16.39356558, 48.18617251], [16.39356558, 48.18617251], [14.21055347, 47.65823679]]
对于非常长的 lon/lat 列表,如何有效地完成此操作?
感谢您的帮助!
为此您需要使用 zip() 函数。像大多数 python3 内置函数一样,它使用迭代器,因此性能非常好。
>>> lon = [14.21055347, 14.21055347, 16.39356558, 16.39356558, 14.21055347]
>>> lat = [48.22824817, 48.22824817, 48.18617251, 48.18617251, 47.65823679]
>>> coordinates = zip(lon, lat)
>>> print(list(coordinates))
[(14.21055347, 48.22824817), (14.21055347, 48.22824817), (16.39356558, 48.18617251), (16.39356558, 48.18617251), (14.21055347, 47.65823679)]
我不太确定我是否正确理解了你的问题,但如果你想匹配这两个数组,只需执行:
名称 = [array1, array2]
您可以利用 map built-in function as well as lambda expressions。
lon = [14.21055347, 14.21055347, 16.39356558, 16.39356558, 14.21055347]
lat = [48.22824817, 48.22824817, 48.18617251, 48.18617251, 47.65823679]
coordinates = list(map(lambda x, y: [x, y], lon, lat))
print(coordinates)
>>> [[14.21055347, 48.22824817], [14.21055347, 48.22824817], [16.39356558, 48.18617251], [16.39356558, 48.18617251], [14.21055347, 47.65823679]]
lon = [14.21055347, 14.21055347, 16.39356558, 16.39356558, 14.21055347]
lat = [48.22824817, 48.22824817, 48.18617251, 48.18617251, 47.65823679]
coordinates = [[i,j] for i,j in zip(lon, lat)]
print(coordinates)
只需使用地图功能
lon = [14.21055347, 14.21055347, 16.39356558, 16.39356558, 14.21055347]
lat = [48.22824817, 48.22824817, 48.18617251, 48.18617251, 47.65823679]
coordinates = list(map(lambda x,y: [x,y], lon,lat))
print(coordinates)
输出是:-
[[14.21055347, 48.22824817], [14.21055347, 48.22824817], [16.39356558, 48.18617251], [16.39356558, 48.18617251], [14.21055347, 47.651][3]8
Map 将函数作为输入,因此必须引入 lambda 函数
我想合并两个列表:
lon: [14.21055347, 14.21055347, 16.39356558, 16.39356558, 14.21055347]
lat: [48.22824817, 48.22824817, 48.18617251, 48.18617251, 47.65823679]
获得:
coordinates: [[14.21055347, 48.22824817], [14.21055347, 48.22824817], [16.39356558, 48.18617251], [16.39356558, 48.18617251], [14.21055347, 47.65823679]]
对于非常长的 lon/lat 列表,如何有效地完成此操作?
感谢您的帮助!
为此您需要使用 zip() 函数。像大多数 python3 内置函数一样,它使用迭代器,因此性能非常好。
>>> lon = [14.21055347, 14.21055347, 16.39356558, 16.39356558, 14.21055347]
>>> lat = [48.22824817, 48.22824817, 48.18617251, 48.18617251, 47.65823679]
>>> coordinates = zip(lon, lat)
>>> print(list(coordinates))
[(14.21055347, 48.22824817), (14.21055347, 48.22824817), (16.39356558, 48.18617251), (16.39356558, 48.18617251), (14.21055347, 47.65823679)]
我不太确定我是否正确理解了你的问题,但如果你想匹配这两个数组,只需执行:
名称 = [array1, array2]
您可以利用 map built-in function as well as lambda expressions。
lon = [14.21055347, 14.21055347, 16.39356558, 16.39356558, 14.21055347]
lat = [48.22824817, 48.22824817, 48.18617251, 48.18617251, 47.65823679]
coordinates = list(map(lambda x, y: [x, y], lon, lat))
print(coordinates)
>>> [[14.21055347, 48.22824817], [14.21055347, 48.22824817], [16.39356558, 48.18617251], [16.39356558, 48.18617251], [14.21055347, 47.65823679]]
lon = [14.21055347, 14.21055347, 16.39356558, 16.39356558, 14.21055347]
lat = [48.22824817, 48.22824817, 48.18617251, 48.18617251, 47.65823679]
coordinates = [[i,j] for i,j in zip(lon, lat)]
print(coordinates)
只需使用地图功能
lon = [14.21055347, 14.21055347, 16.39356558, 16.39356558, 14.21055347]
lat = [48.22824817, 48.22824817, 48.18617251, 48.18617251, 47.65823679]
coordinates = list(map(lambda x,y: [x,y], lon,lat))
print(coordinates)
输出是:- [[14.21055347, 48.22824817], [14.21055347, 48.22824817], [16.39356558, 48.18617251], [16.39356558, 48.18617251], [14.21055347, 47.651][3]8
Map 将函数作为输入,因此必须引入 lambda 函数