首页 > 解决方案 > 如何在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]]

标签: pythonappendsequential

解决方案


如果你不介意元组,你可以使用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],
...

推荐阅读