Python:對列表進行排序並相應地更改另一個列表?


我有兩個列表:一個包含一組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]

來自:https://cloud.tencent.com/developer/ask/54457


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM