Py学习  »  Python

python排序字典列表

VengaVenga • 6 年前 • 1426 次点击  

我有一个字典列表:

AccountValues = [
{'portfolio_ref': 1, 'tag': 'FullInit', 'value': '20642.95', 'currency': 'USD', 'percent': 0.0}, 
{'portfolio_ref': 1, 'tag': 'FullMaint', 'value': '21350.54', 'currency': 'USD', 'percent': 0.0}, 
{'portfolio_ref': 1, 'tag': 'NetLiq', 'value': '70976.05', 'currency': 'USD', 'percent': 100.0} ]

按SQL描述的简单任务:按组合排序\uRef asc,百分比描述

我的尝试没有成功:

sorted(AccountsValues, key=lambda x: (x[1],-x[4]))

给我

KeyError: 1

第二次尝试:

import operator
result = sorted(myAccountsValues, key=itemgetter('percent'))

不能按百分比排序。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/30637
 
1426 次点击  
文章 [ 2 ]  |  最新文章 6 年前