本文介绍了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:对列表进行数字排序
发布评论