我如何在python中按顺序添加两个列表?

问题描述 投票:-1回答: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]]
python append sequential
2个回答
2
投票

如果您不介意元组,则只需使用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],
...

0
投票
[list(item) for item in list(zip(coor,height))]
© www.soinside.com 2019 - 2024. All rights reserved.