简体   繁体   English

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

[英]Making a list of [x, y] points from separate lists of [X] and [Y]

I have two lists: 我有两个清单:

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

And I want to end up with a tuple of lists of the individual points, [x_i, y_i], like this: ([0, 10],[50, 20],[100, 30]) 我想以一个单个点[x_i,y_i]的列表的元组结束,像这样:([0,10],[50,20],[100,30])

Is there an easier or more pythonic way than this enumeration? 有没有比此枚举更简单或更Python的方式?

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

Use zip . 使用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])

Or: 要么:

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

This is extracted from the answer in the following post: How to merge lists into a list of tuples? 这是从以下文章的答案中提取的: 如何将列表合并到元组列表中?

>>> 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)]

你甚至可以做

result=[(x_points[i],y_points[i]) for i in range(len(x_points))]
x_points = [0, 50, 100]
y_points = [10, 20, 30]
result = tuple(map(list,zip(x_points, y_points)))

print(result)

output 输出

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM