简体   繁体   English

比较列表中嵌套字典的最佳方法,Python

[英]Best way to compare nested dictionaries in a list, Python

I'm querying a public endpoint to get an exchange rate of varies exchanges that returns a list with nested dictionaries.我正在查询公共端点以获取返回带有嵌套字典的列表的各种交换的汇率。 I'm most interesting in is the key "amount" field in the nested dictionaries.我最感兴趣的是嵌套字典中的关键“金额”字段。 I'm struggling to come up with a solution to store the nested dictionary that has the most "amount" value in a variable.我正在努力想出一个解决方案来存储在变量中具有最多“数量”值的嵌套字典。 Any ideas would be extremely helpful.任何想法都会非常有帮助。 I'm banging my head against the wall on this one.我正在用头撞墙。

Here is the list:这是清单:

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":""}]

The dictionary that has the most amount is:数量最多的字典是:

{"partner":"stealthex","amount":37.90346104,"currency":"cel","supportRate":2,"duration":66.62083333333334,"fixed":true,"min":39.91443225,"max":2550.215226,"exists":true,"id":""}

Use the key parameter of max :使用maxkey参数:

from operator import  itemgetter
res = max(list_with_nested_dicts, key=itemgetter("amount"))
print(res)

Output输出

{'partner': 'stealthex', 'amount': 37.90346104, 'currency': 'cel', 'supportRate': 2, 'duration': 66.62083333333334, 'fixed': True, 'min': 39.91443225, 'max': 2550.215226, 'exists': True, 'id': ''}

From the documentation:从文档:

The key argument specifies a one-argument ordering function like that used for list.sort() key 参数指定一个单参数排序函数,就像用于 list.sort()

Note there is no need to use operator.itemgetter , you could simply use a lambda function as the value of key :请注意,不需要使用operator.itemgetter ,您可以简单地使用 lambda 函数作为key的值:

max(list_with_nested_dicts, key=lambda x: x["amount"])

Your dict is not a valid python expression, eg, true is not defined (unless you have defined it before).您的 dict 不是有效的 Python 表达式,例如,未定义true (除非您之前已定义)。 You might want to replace true with True and false with False .您可能想用True替换true ,用False替换false

You can use max with key parameter:您可以将maxkey参数一起使用:

from operator import itemgetter
d = max(list_with_nested_dicts, key=itemgetter('amount'))
print(d) # 083333333334, 'fixed': True, 'min': 39.91443225, 'max': 2550.215226, 'exists': True, 'id': ''}

Try this: Bu you will need to replace 'true' with True and 'false' with 'False' for this code to work.试试这个: 但是你需要用 True 替换 'true',用 'False' 替换 'false' 才能使这段代码正常工作。

list_with_nested_dicts.sort(key=lambda a: a['amount'])
print(list_with_nested_dicts[-1])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM