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
Post a Comment