myDict = { 'item1' : [ 7, 1, 9], 'item2' : [8, 2, 3], 'item3' : [ 9, 3, 11 ] }
函数:
def sortDic(Dict,valuePostion):
return sorted(Dict.items(),key=lambda e:e[1][valuePostion],reverse=False) //reverse=True 倒排
1. 按value的第3个值排序
sortDic(myDict,2)
[('item2', [8, 2, 3]), ('item1', [7, 1, 9]), ('item3', [9, 3, 11])]
2. 按value的第1个值排序
sortDic(myDict,0)
[('item1', [7, 1, 9]), ('item2', [8, 2, 3]), ('item3', [9, 3, 11])]
本文介绍了一种使用Python对字典进行排序的方法,可以根据字典value中特定位置的元素来进行升序或降序排列。通过具体示例展示了如何针对不同位置的数据进行排序。
1937

被折叠的 条评论
为什么被折叠?



