Python:对列表进行数字排序

编程入门 行业动态 更新时间:2024-10-16 02:24:06
本文介绍了Python:对列表进行数字排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个x,y坐标列表,我需要根据x坐标进行排序,然后在x相同时再选择y坐标,并消除相同坐标的重复项.例如,如果列表为:

I have a list of x,y coordinates that I need to sort based on the x coordinate, then y coordinate when x is the same and eliminate duplicates of the same coordinates. For example, if the list is:

[[450.0, 486.6], [500.0, 400.0], [450.0, 313.3], [350.0, 313.3], [300.0, 400.0], [349.9, 486.6], [450.0, 313.3]]

我需要将其重新排列为:

I would need to rearrange it to:

[[300.0, 400.0], [349.9, 486.6], [350.0, 313.3], [450.0, 313.3], [450.0, 486.6], [500.0, 400.0]]

(已删除重复的[450.0, 313.3])

推荐答案

无论如何,这是列表列表的正常排序顺序.用字典将其删除.

That is the normal sort order for a list of lists, anyway. De-dupe it with a dict.

>>> L = [[450.0, 486.6], [500.0, 400.0], [450.0, 313.3], [350.0, 313.3], [300.0, 400.0], [349.9, 486.6], [450.0, 313.3]] >>> sorted({tuple(x): x for x in L}.values()) [[300.0, 400.0], [349.9, 486.6], [350.0, 313.3], [450.0, 313.3], [450.0, 486.6], [500.0, 400.0]]

更多推荐

Python:对列表进行数字排序

本文发布于:2023-11-29 15:58:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1646870.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数字   列表   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!