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

ruby on rails - Permission denied @ sys_fail2 - (D:/RoR/projects/grp/public/uploads/ -

c++ - nodejs socket.io closes connection before upgrading to websocket -

java - What is the equivalent of @Value in CDI world? -