如何将两个列表相同位置的元素放在一起组成一个列表列表?

时间:2021-07-16 20:02:43

标签: python list

我现在有两个列表如下所示:

Coord_leaking_pipes_x_coords:  [387.02, 1899.68, 2485.55, 1167.79, 296.91, 937.42, 1293.7, 267.81, 540.25, 1112.86, 1249.76, 1655.71, 2413.3, 2691.82]
Coord_leaking_pipes_y_coords:  [956.05, 803.57, 632.88, 713.02, 1569.97, 1141.56, 960.69, 423.37, 317.17, 345.85, 430.96, 657.27, 397.27, 842.85]

我想得到下图所示的新列表,就是把上面两个列表的相同位置的元素放在一起组成一个列表,把这些新列表放到一个大列表中。

Coord_leaking_pipes_coords; [[387.02,956.05],[1899.68,803.57],[2485.55,632.88],[1167.79,713.02],......,[2691.82,842.85]]

你能告诉我如何实现它吗?

2 个答案:

答案 0 :(得分:0)

x = [387.02, 1899.68, 2485.55, 1167.79, 296.91, 937.42, 1293.7, 267.81, 540.25, 1112.86, 1249.13,16, 26, 16.56]

y = [956.05, 803.57, 632.88, 713.02, 1569.97, 1141.56, 960.69, 423.37, 317.17, 345.85, 430.96, 725, 725, 85]

result = [[x[i],y[i]] for i in range(len(x))]

答案 1 :(得分:0)

这是使用内置 zip() 函数的一种方式:

x_coords = [387.02, 1899.68, 2485.55, 1167.79, 296.91, 937.42, 1293.7, 267.81, 540.25, 1112.86, 1249.76, 1655.71, 2413.3, 2691.82]
y_coords = [956.05, 803.57, 632.88, 713.02, 1569.97, 1141.56, 960.69, 423.37, 317.17, 345.85, 430.96, 657.27, 397.27, 842.85]

coords = list(list(t) for t in zip(x_coords, y_coords))
相关问题