从 [X] 和 [Y] 的单独列表中制作 [x, y] 点的列表
Making a list of [x, y] points from separate lists of [X] and [Y]
我有两个列表:
x_points = [0, 50, 100]
y_points = [10, 20, 30]
我想以单个点列表的元组结束,[x_i,y_i],像这样:([0, 10],[50, 20] ,[100, 30])
是否有比此枚举更简单或更 pythonic 的方法?
result = tuple([x, y_points[i]] for i, x in enumerate(x_points))
使用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)))
这是从下面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)
输出
([0, 10], [50, 20], [100, 30])
我有两个列表:
x_points = [0, 50, 100]
y_points = [10, 20, 30]
我想以单个点列表的元组结束,[x_i,y_i],像这样:([0, 10],[50, 20] ,[100, 30])
是否有比此枚举更简单或更 pythonic 的方法?
result = tuple([x, y_points[i]] for i, x in enumerate(x_points))
使用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)))
这是从下面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)
输出
([0, 10], [50, 20], [100, 30])