从[X]和[Y]的单独列表中列出[x,y]点的列表

时间:2019-07-03 05:05:55

标签: python list tuples enumeration

我有两个列表:

x_points = [0, 50, 100]
y_points = [10, 20, 30]

最后,我想得出一个单个点[x_i,y_i]的列表的元组,像这样:([0,10],[50,20],[100,30])

是否有比此枚举更简单或更Python化的方式?

result = tuple([x, y_points[i]] for i, x in enumerate(x_points))

4 个答案:

答案 0 :(得分:2)

使用zip

x_points = [0, 50, 100]
y_points = [10, 20, 30]

print(tuple([x, y] for x, y in zip(x_points, y_points)))
# ([0, 10], [50, 20], [100, 30])

或者:

tuple(map(list, zip(x_points, y_points)))

答案 1 :(得分:0)

这是从以下帖子的答案中摘录的:How to merge lists into a list of tuples?

goto()

答案 2 :(得分:0)

你甚至可以做

>>> list_a = [1, 2, 3, 4]
>>> list_b = [5, 6, 7, 8]
>>> list(zip(list_a, list_b))
[(1, 5), (2, 6), (3, 7), (4, 8)]

答案 3 :(得分:0)

x_points = [0, 50, 100]
y_points = [10, 20, 30]
result = tuple(map(list,zip(x_points, y_points)))

print(result)

输出

([0, 10], [50, 20], [100, 30])