如何在 python 中按顺序附加两个列表?
How can I append two lists in sequential order in python?
我有一个坐标列表和另一个高度值列表。如何按顺序将高度值附加到坐标列表?
coor = [[[83.75, 18.70], [57.50, 18.70], [57.5, 2.87], [83.75, 4.18]],
[[83.75, 34.54], [110.0, 35.0], [104.86, 19.59], [83.75, 19.59]],
[[104.86, 19.59], [83.75, 19.59], [83.75, 4.18], [100.0, 5.0]],
[[-5.0, 33.0], [18.12, 33.40],[18.12, 16.70],[-2.53, 16.70]],
[[18.12, 16.70],[-2.53, 16.70], [0.0, 0.0],[18.12, 0.90]]]
height = [5,4,5,6,6]
预期结果:
result = [[[83.75, 18.70], [57.50, 18.70], [57.5, 2.87], [83.75, 4.18],5],
[[83.75, 34.54], [110.0, 35.0], [104.86, 19.59], [83.75, 19.59],4],
[[104.86, 19.59], [83.75, 19.59], [83.75, 4.18], [100.0, 5.0],5],
[[-5.0, 33.0], [18.12, 33.40],[18.12, 16.70],[-2.53, 16.70],6],
[[18.12, 16.70],[-2.53, 16.70], [0.0, 0.0],[18.12, 0.90],6]]
如果你不介意元组,你可以使用zip
> list(zip(coor, height))
[([[83.75, 18.7], [57.5, 18.7], [57.5, 2.87], [83.75, 4.18]], 5),
([[83.75, 34.54], [110.0, 35.0], [104.86, 19.59], [83.75, 19.59]], 4),
...
如果它必须是一个列表,请在理解中使用 zip
。
> [list(pair) for pair in zip(coor, height)]
[[[83.75, 18.7], [57.5, 18.7], [57.5, 2.87], [83.75, 4.18]], 5],
[[[83.75, 34.54], [110.0, 35.0], [104.86, 19.59], [83.75, 19.59]], 4],
...
tldr:一行解决方案,zip 可迭代对象(Python docs)和下面列出的所需输出说明:
[list(item) for item in list(zip(coor,height))]
解释:
list1 = [
[['a','a'],['aa','aa']],
[['b','b'],['bb','bb']]]
list2 = [1,2]
for item in list(zip(list1,list2)):
print('zip output', item)
print('desired output', list(item))
输出:
zip output ([['a', 'a'], ['aa', 'aa']], 1)
desired output [[['a', 'a'], ['aa', 'aa']], 1]
zip output ([['b', 'b'], ['bb', 'bb']], 2)
desired output [[['b', 'b'], ['bb', 'bb']], 2]
作为一行列表理解:
[list(item) for item in list(zip(list1,list2))]
输出:
[[[['a', 'a'], ['aa', 'aa']], 1], [[['b', 'b'], ['bb', 'bb']], 2]]
我有一个坐标列表和另一个高度值列表。如何按顺序将高度值附加到坐标列表?
coor = [[[83.75, 18.70], [57.50, 18.70], [57.5, 2.87], [83.75, 4.18]],
[[83.75, 34.54], [110.0, 35.0], [104.86, 19.59], [83.75, 19.59]],
[[104.86, 19.59], [83.75, 19.59], [83.75, 4.18], [100.0, 5.0]],
[[-5.0, 33.0], [18.12, 33.40],[18.12, 16.70],[-2.53, 16.70]],
[[18.12, 16.70],[-2.53, 16.70], [0.0, 0.0],[18.12, 0.90]]]
height = [5,4,5,6,6]
预期结果:
result = [[[83.75, 18.70], [57.50, 18.70], [57.5, 2.87], [83.75, 4.18],5],
[[83.75, 34.54], [110.0, 35.0], [104.86, 19.59], [83.75, 19.59],4],
[[104.86, 19.59], [83.75, 19.59], [83.75, 4.18], [100.0, 5.0],5],
[[-5.0, 33.0], [18.12, 33.40],[18.12, 16.70],[-2.53, 16.70],6],
[[18.12, 16.70],[-2.53, 16.70], [0.0, 0.0],[18.12, 0.90],6]]
如果你不介意元组,你可以使用zip
> list(zip(coor, height))
[([[83.75, 18.7], [57.5, 18.7], [57.5, 2.87], [83.75, 4.18]], 5),
([[83.75, 34.54], [110.0, 35.0], [104.86, 19.59], [83.75, 19.59]], 4),
...
如果它必须是一个列表,请在理解中使用 zip
。
> [list(pair) for pair in zip(coor, height)]
[[[83.75, 18.7], [57.5, 18.7], [57.5, 2.87], [83.75, 4.18]], 5],
[[[83.75, 34.54], [110.0, 35.0], [104.86, 19.59], [83.75, 19.59]], 4],
...
tldr:一行解决方案,zip 可迭代对象(Python docs)和下面列出的所需输出说明:
[list(item) for item in list(zip(coor,height))]
解释:
list1 = [
[['a','a'],['aa','aa']],
[['b','b'],['bb','bb']]]
list2 = [1,2]
for item in list(zip(list1,list2)):
print('zip output', item)
print('desired output', list(item))
输出:
zip output ([['a', 'a'], ['aa', 'aa']], 1)
desired output [[['a', 'a'], ['aa', 'aa']], 1]
zip output ([['b', 'b'], ['bb', 'bb']], 2)
desired output [[['b', 'b'], ['bb', 'bb']], 2]
作为一行列表理解:
[list(item) for item in list(zip(list1,list2))]
输出:
[[[['a', 'a'], ['aa', 'aa']], 1], [[['b', 'b'], ['bb', 'bb']], 2]]