[英]How to get Python dictionary with the highest key value?
I have a dictionary with latitude and longitude values passed from a different file.我有一本字典,其中包含从不同文件传递的纬度和经度值。
absoluteList = {"latitude": absolute[1], "longitude": absolute[0]}
If we print absoluteList
we get:如果我们打印absoluteList
我们得到:
{'latitude': 507476711, 'longitude': -18299961}
{'latitude': 507447383, 'longitude': -18388366}
{'latitude': 507436793, 'longitude': -18459606}
{'latitude': 507427288, 'longitude': -18500804}
{'latitude': 507410993, 'longitude': -18521404}
{'latitude': 507395241, 'longitude': -18552732}
{'latitude': 507362921, 'longitude': -18550157}
{'latitude': 507344995, 'longitude': -18521404}
I need to print out the dictionary with the highest latitude value.我需要打印出纬度值最高的字典。
absoluteList = [
{'latitude': 507476711, 'longitude': -18299961},
{'latitude': 507447383, 'longitude': -18388366},
{'latitude': 507436793, 'longitude': -18459606},
{'latitude': 507427288, 'longitude': -18500804},
{'latitude': 507410993, 'longitude': -18521404},
{'latitude': 507395241, 'longitude': -18552732},
{'latitude': 507362921, 'longitude': -18550157},
{'latitude': 507344995, 'longitude': -18521404}
]
biggest_latitude = max(absoluteList, key=lambda x: x['latitude'])
{'latitude': 507476711, 'longitude': -18299961}
print( max(absoluteList, key=lambda x: x['latitude']))
The are 2 ways to do it is by有两种方法是通过
using itemgetter()
使用itemgetter()
from operator import itemgetter highest_value_in_list = max(absoluteList, key=itemgetter('latitude'))`
using max
:使用max
:
highest_value_in_list = max(absoluteList, key=lambda x: x['latitude'])
output: output:
{'latitude': 507476711, 'longitude': -18299961}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.