我正在查询一个公共端点,以获取各种交换的汇率,该汇率返回一个带有嵌套字典的列表。我最感兴趣的是关键的"数量"字段。我正在努力想出一个解决方案来存储具有最多"数量"的嵌套字典。变量中的值。任何想法都会非常有帮助。在这件事上我真是白费力气。
列表如下:
list_with_nested_dicts = [{"partner":"simpleswap","amount":0,"currency":"cel","supportRate":3,"duration":0,"fixed":true,"min":0,"max":0,"exists":false,"id":""},{"partner":"simpleswap","amount":0,"currency":"cel","supportRate":3,"duration":0,"fixed":false,"min":0,"max":0,"exists":false,"id":""},{"partner":"stealthex","amount":37.90346104,"currency":"cel","supportRate":2,"duration":66.62083333333334,"fixed":true,"min":39.91443225,"max":2550.215226,"exists":true,"id":""},{"partner":"stealthex","amount":37.20972396,"currency":"cel","supportRate":2,"duration":23.158333333333335,"fixed":false,"min":77.82938688,"max":25209.49720665,"exists":true,"id":""},{"partner":"godex","amount":0,"currency":"cel","supportRate":0,"duration":0,"fixed":true,"min":0,"max":0,"exists":false,"id":""},{"partner":"changenow","amount":37.2077365,"currency":"cel","supportRate":2,"duration":11.08859649122807,"fixed":false,"min":56.60646516,"max":25259.88795509,"exists":true,"id":""},{"partner":"changelly","amount":0,"currency":"cel","supportRate":1,"duration":0,"fixed":true,"min":0,"max":0,"exists":false,"id":""},{"partner":"changelly","amount":0,"currency":"cel","supportRate":1,"duration":0,"fixed":false,"min":0,"max":0,"exists":false,"id":""},{"partner":"instaswap","amount":0,"currency":"cel","supportRate":2,"duration":0,"fixed":false,"min":0,"max":0,"exists":false,"id":""},{"partner":"exolix","amount":0,"currency":"cel","supportRate":0,"duration":0,"fixed":true,"min":0,"max":0,"exists":false,"id":""},{"partner":"fixedfloat","amount":0,"currency":"cel","supportRate":3,"duration":0,"fixed":false,"min":0,"max":0,"exists":false,"id":""},{"partner":"switchain","amount":0,"currency":"cel","supportRate":2,"duration":0,"fixed":true,"min":0,"max":0,"exists":false,"id":""},{"partner":"changehero","amount":0,"currency":"cel","supportRate":3,"duration":56.41111111111111,"fixed":true,"min":0,"max":0,"exists":false,"id":""},{"partner":"changehero","amount":0,"currency":"cel","supportRate":3,"duration":2.4833333333333334,"fixed":false,"min":0,"max":0,"exists":false,"id":""},{"partner":"binance","amount":0,"currency":"cel","supportRate":2,"duration":0,"fixed":false,"min":0,"max":0,"exists":false,"id":""},{"partner":"nexchange","amount":0,"currency":"cel","supportRate":3,"duration":0,"fixed":true,"min":0,"max":0,"exists":false,"id":""},{"partner":"letsexchange","amount":0,"currency":"cel","supportRate":2,"duration":0,"fixed":true,"min":0,"max":0,"exists":false,"id":""},{"partner":"letsexchange","amount":0,"currency":"cel","supportRate":2,"duration":0,"fixed":false,"min":0,"max":0,"exists":false,"id":""},{"partner":"alfacash","amount":0,"currency":"cel","supportRate":3,"duration":0,"fixed":true,"min":0,"max":0,"exists":false,"id":""}]
数量最多的字典是:
{"partner":"stealthex","amount":37.90346104,"currency":"cel","supportRate":2,"duration":66.62083333333334,"fixed":true,"min":39.91443225,"max":2550.215226,"exists":true,"id":""}
使用max
的key
参数:
from operator import itemgetter
res = max(list_with_nested_dicts, key=itemgetter("amount"))
print(res)
{'partner': 'stealthex', 'amount': 37.90346104, 'currency': 'cel', 'supportRate': 2, 'duration': 66.62083333333334, 'fixed': True, 'min': 39.91443225, 'max': 2550.215226, 'exists': True, 'id': ''}
来自文档:
key参数指定了一个单参数排序函数用于list.sort()
注意,不需要使用operator.itemgetter
,您可以简单地使用lambda函数作为key
的值:
max(list_with_nested_dicts, key=lambda x: x["amount"])
此外,如果某些字典没有"amount"
项,我建议您使用.get
,如下所示:
default = 0
res = max(list_with_nested_dicts, key=lambda x: x.get("amount", default))
其中default
应该是最适合您需求的值。
试试这个:但是你需要将'true'替换为true,将'false'替换为'false'才能使此代码正常工作。
list_with_nested_dicts.sort(key=lambda a: a['amount'])
print(list_with_nested_dicts[-1])