我有兩個列表:一個包含一組x點,另一個包含y點。我需要按照從最低到最高的順序對x點的列表進行排序,並且移動y點以跟隨它們的x個對應點。
x = [3,2,1]
y = [1,2,3]
points = zip(x,y)
points
[(3, 1), (2, 2), (1, 3)]
sorted(points)
[(1, 3), (2, 2), (3, 1)]
然后再打開它們:
sorted_points = sorted(points)
new_x = [point[0] for point in sorted_points]
new_y = [point[1] for point in sorted_points]
new_x
[1, 2, 3]
new_y
[3, 2, 1]