python sorted用法

>>> a
{'a': [1, 3], 'b': [2, 4]}
>>> a.items()
[('a', [1, 3]), ('b', [2, 4])]
>>> sorted(a.items(),key=lambda x:x[1][2])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 1, in <lambda>
IndexError: list index out of range
>>> sorted(a.items(),key=lambda x:x[1][1])
[('a', [1, 3]), ('b', [2, 4])]
>>> sorted(a.items(),key=lambda x:x[1][1],reverse=True)
[('b', [2, 4]), ('a', [1, 3])]
>>>

原文地址:https://www.cnblogs.com/lexus/p/2768649.html