Tuesday, 15 July 2014

order - Ordering a multidimensional array in python -



order - Ordering a multidimensional array in python -

this question has reply here:

how sort list of lists specific index of inner list? 4 answers

i have info construction like:

[ ["id1", 123], ["id2", 1], ["id3", 6] ]

and ordered (descendingly) according sec variable, this:

[ ["id1", 123], ["id3", 6], ["id2", 1] ]

i write little function doing that, sure there's cool 1 liner way of doing it, isn't there? thanks.

you can using sorted , itemgetter:

>>> = [ ["id1", 123], ["id2", 1], ["id3", 6] ] >>> operator import itemgetter >>> sorted(a, key=itemgetter(1), reverse=true) [['id1', 123], ['id3', 6], ['id2', 1]]

if purely wanted one-liner (no import), can lambda it:

>>> sorted(a, key=lambda l: l[1], reverse=true)

python order

No comments:

Post a Comment