Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k points)

I have a list of x,y coordinates and I want to sort them 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 want 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]]

Can anyone tell me the solution?

1 Answer

0 votes
by (106k points)

To sort the list of lists numerically in Python you can use the below-mentioned way.

>>> 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]]

Related questions

0 votes
4 answers
0 votes
1 answer
0 votes
4 answers
0 votes
2 answers
asked Jul 22, 2019 in Python by Sammy (47.6k points)

Browse Categories

...