简体   繁体   English

如何使用嵌套字典项中存储的时间查找嵌套字典中的最新条目? (Python)

[英]How do I find the latest entry in a nested dictionary using the time stored in nested dictionary items? (Python)

I have a dictionary that looks like this (the dictionary has 4.000+ entries)我有一本看起来像这样的字典(字典有 4.000 多个条目)

{'order': {'T2DZVX-OD7YQ3':  {'cost': '19.95',
                              'ordertxid': 'OYKRY2-E6VJZ',
                              'time': 1649666120.6187882,
                              },
           'T2V5ZF-U7VCII':  {'cost': '14.95',
                              'ordertxid': 'QVLNT-BRSIQX',
                              'time': 1650276000.0017374,
                              },
           'T47WOA-5GR52B':  {'cost': '24.95',
                              'ordertxid': 'OVT5LL-WHDX3',
                              'time': 1649366792.625624}}}

How do I find the key?如何找到钥匙? In this case "T2V5ZF-U7VCII" where the 'time' is the highest number?在这种情况下,“T2V5ZF-U7VCII”的“时间”是最高数字? (newest entry) (最新条目)

Use max on the different orders pairs using the good sorting key使用良好的排序键在不同的订单对上使用max

values = {'order': {
    'T2DZVX-OD7YQ3': {'cost': '19.95', 'ordertxid': 'OYKRY2-E6VJZ', 'time': 1649666120.6187882, },
    'T2V5ZF-U7VCII': {'cost': '14.95', 'ordertxid': 'QVLNT-BRSIQX', 'time': 1650276000.0017374, },
    'T47WOA-5GR52B': {'cost': '24.95', 'ordertxid': 'OVT5LL-WHDX3', 'time': 1649366792.625624}}
}

x = max(values['order'].items(), key=lambda x: x[1]['time'])
print(x)  # ('T2V5ZF-U7VCII', {'cost': '14.95', 'ordertxid': 'QVLNT-BRSIQX', 'time': 1650276000.0017374})
print(x[0])  # T2V5ZF-U7VCII

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

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