Thursday, September 22, 2011

sorting in python by arbitrary key

E.g. we have a list of dictionaries

x = [{'a':1,'b':3}, {'a':3,'b':2}, {'a':2,'b':1}]
x.sort(key=lambda i: i['b'])

sorts it by value at the 'b' key

No comments:

Post a Comment

Checking font availability on Mac   fc-list : family | grep "Fira Code"   or   system_profiler -json SPFontsDataType | grep \...