python - How to sort a list of x-y coordinates -


i need sort list of [x,y] coordinates looks this:

list = [[1,2],[0,2],[2,1],[1,1],[2,2],[2,0],[0,1],[1,0],[0,0]] 

the pattern i'm looking after sorting is:

[x,y] coordinate shall sorted y first , x. new list should like:

list = [[0,0],[1,0],[2,0],[0,1],[1,1],[2,1],[0,2],[1,2],[2,2]] 

i can't figure out how , appreciate help.

use sorted key:

>>> my_list = [[1,2],[0,2],[2,1],[1,1],[2,2],[2,0],[0,1],[1,0],[0,0]] >>> sorted(my_list , key=lambda k: [k[1], k[0]]) [[0, 0], [1, 0], [2, 0], [0, 1], [1, 1], [2, 1], [0, 2], [1, 2], [2, 2]] 

it first sort on y value , if that's equal sort on x value.

i advise not use list variable because built-in data structure.


Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -