01: study range / sort / lambda
Sort the following data
raw data:
[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5]
Target data:
[0, -1, 1, -2, 2, -3, 3, -4, 4, -5, 5]
Implementation code
sorted(range(-5, 6), key=lambda x: abs(x))
02 sort of dictionary of key / value
data = dict(a=2, b=4, c=1, d=3)
# 按照key进行排序 sorted(data.items(), key=lambda x: x[0]) # [('a', 2), ('b', 4), ('c', 1), ('d', 3)] # 按照value进行排序 sorted(data.items(), key=lambda x: x[1]) # [('c', 1), ('a', 2), ('d', 3), ('b', 4)] # 逆序 sorted(data.items(), key=lambda x: x[1], reverse=True) [('b', 4), ('d', 3), ('a', 2), ('c', 1)]
Source:
http://www.1994july.club/seo/