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