标签:展示 print style pre 排序 value 结果 item col
1 li = [{‘a‘:1,‘b‘:4},{‘a‘:2,‘b‘:3},{‘a‘:3,‘b‘:2},{‘a‘:4,‘b‘:1}] 2 print(‘li‘,li) 3 #根据a的值倒序排序 4 liNew = sorted(li,key=lambda e:e.__getitem__(‘a‘),reverse=True) 5 print(‘liNew‘,liNew) 6 #根据a的值倒序排序 7 liNew2 = sorted(li,key=lambda e:e[‘a‘],reverse=True) 8 print(‘liNew2‘,liNew2)
结果展示:
1 li [{‘a‘: 1, ‘b‘: 4}, {‘a‘: 2, ‘b‘: 3}, {‘a‘: 3, ‘b‘: 2}, {‘a‘: 4, ‘b‘: 1}] 2 liNew [{‘a‘: 4, ‘b‘: 1}, {‘a‘: 3, ‘b‘: 2}, {‘a‘: 2, ‘b‘: 3}, {‘a‘: 1, ‘b‘: 4}] 3 liNew2 [{‘a‘: 4, ‘b‘: 1}, {‘a‘: 3, ‘b‘: 2}, {‘a‘: 2, ‘b‘: 3}, {‘a‘: 1, ‘b‘: 4}]
标签:展示 print style pre 排序 value 结果 item col
原文地址:https://www.cnblogs.com/whycai/p/12173229.html