[英]MAX or latest Value from a list of dict in Python
I have a list of dictionary as below我有一个字典列表如下
[
{'ClientId': 70433, 'ReportDate': '2020-05-01', 'JobId': 389512, 'AccountPnlCount': 507, 'DeskPnlCount': 545, 'ThresholdName': 'Threshold2', 'RuleName': 'PnlRowCountCheck', 'StartTime': datetime.datetime(2020, 5, 1, 21, 8, 32, 940000), 'CSTTimeStr': '2020-05-01T21:08:32.940000Z'},
{'ClientId': 70433, 'ReportDate': '2020-05-01', 'JobId': 389525, 'AccountPnlCount': 507, 'DeskPnlCount': 545, 'ThresholdName': 'Threshold2', 'RuleName': 'PnlRowCountCheck', 'StartTime': datetime.datetime(2020, 5, 2, 0, 15, 21, 820000), 'CSTTimeStr': '2020-05-02T00:15:21.820000Z'}
]
I need the latest record JobId number Based on StartTime.我需要基于 StartTime 的最新记录 JobId 编号。 ie for the MAX of "StartTime" I need the corresponding JobId, which is 389525.
即对于“StartTime”的 MAX,我需要相应的 JobId,即 389525。
Can you please assist?你能帮忙吗? thank You
谢谢你
Use max
with a key
argument to find the dictionary, then just get the JobId
out of that dictionary:使用带
key
参数的max
来查找字典,然后从该字典中获取JobId
:
>>> derp
[{'ClientId': 70433, 'ReportDate': '2020-05-01', 'JobId': 389512, 'AccountPnlCount': 507, 'DeskPnlCount': 545, 'ThresholdName': 'Threshold2', 'RuleName': 'PnlRowCountCheck', 'StartTime': datetime.datetime(2020, 5, 1, 21, 8, 32, 940000), 'CSTTimeStr': '2020-05-01T21:08:32.940000Z'}, {'ClientId': 70433, 'ReportDate': '2020-05-01', 'JobId': 389525, 'AccountPnlCount': 507, 'DeskPnlCount': 545, 'ThresholdName': 'Threshold2', 'RuleName': 'PnlRowCountCheck', 'StartTime': datetime.datetime(2020, 5, 2, 0, 15, 21, 820000), 'CSTTimeStr': '2020-05-02T00:15:21.820000Z'}]
>>> max(derp, key=lambda d: d['StartTime'])['JobId']
389525
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.