如何获取JSON列表的最后一个元素

时间:2018-03-16 10:02:04

标签: python json filtering

我遇到了一个简单的问题:

我通过urllib获得了一个JSON应用列表,如下所示:

    "completedapps" : [ {
    "starttime" : 1520863179923,
    "id" : "app-20180312145939-0183",
    "name" : "IE_Traitement_3",
    "cores" : 1,
    "user" : "root",
    "memoryperslave" : 1024,
    "submitdate" : "Mon Mar 12 14:59:39 CET 2018",
    "state" : "FINISHED",
    "duration" : 212967
  }, {
    "starttime" : 1520863398147,
    "id" : "app-20180312150318-0186",
    "name" : "IE_Traitement_3",
    "cores" : 1,
    "user" : "root",
    "memoryperslave" : 1024,
    "submitdate" : "Mon Mar 12 15:03:18 CET 2018",
    "state" : "FINISHED",
    "duration" : 6321
  }, {
    "starttime" : 1520863387941,
    "id" : "app-20180312150307-0185",
    "name" : "IE_Traitement_0A",
    "cores" : 1,
    "user" : "root",
    "memoryperslave" : 1024,
    "submitdate" : "Mon Mar 12 15:03:07 CET 2018",
    "state" : "FINISHED",
    "duration" : 149536
  }, { ... }]

我想获取名为“IE_Traitement_OA”的应用程序的最新元素,所以我开始像这样过滤我的JSON:

[app for app in parsedjson['completedapps'] if app['name'] == "IE_Traitement_OA"]

但我现在被困住了,我不知道我怎么能得到最新的“app”?我想我必须使用starttimesubmitdate字段,但我不知道如何处理。你能帮帮我吗?

3 个答案:

答案 0 :(得分:1)

您可以使用以下内容进行过滤:

a = list(filter(lambda x: x['name'] == 'IE_Traitement_0A', data['completedapps']))

a将包含与您的过滤器匹配的所有字典的列表,然后您可以将列表排序为最新的 - 使用任何键对其进行排序

sorted_a = sorted(a, key=lambda k: k['starttime'])

如果你只想要一个,那么选择sorted_a的第一个元素,假设它不是空的。

编辑:使用min而不是排序感谢提示@VPfB

min_a = min(a, key=lambda k: k['starttime'])

答案 1 :(得分:0)

如果您使用starttime,可以使用max功能:

data = [{
    "starttime" : 1520863398147,
    "id" : "app-20180312150318-0186",
    "name" : "IE_Traitement_3",
    "cores" : 1,
    "user" : "root",
    "memoryperslave" : 1024,
    "submitdate" : "Mon Mar 12 15:03:18 CET 2018",
    "state" : "FINISHED",
    "duration" : 6321
}, {
    "starttime" : 1520863387941,
    "id" : "app-20180312150307-0185",
    "name" : "IE_Traitement_0A",
    "cores" : 1,
    "user" : "root",
    "memoryperslave" : 1024,
    "submitdate" : "Mon Mar 12 15:03:07 CET 2018",
    "state" : "FINISHED",
    "duration" : 149536
}]

most_recent = max(data,key=lambda e: e['starttime'])
print(most_recent)

现在,如果你想使用submitdate,你需要先转换

在此链接中有一些转化示例:Converting string into datetime

好看!

答案 2 :(得分:0)

req_json = """{"completedapps" : [ {
    "starttime" : 1520863179923,
    "id" : "app-20180312145939-0183",
    "name" : "IE_Traitement_3",
    "cores" : 1,
    "user" : "root",
    "memoryperslave" : 1024,
    "submitdate" : "Mon Mar 12 14:59:39 CET 2018",
    "state" : "FINISHED",
    "duration" : 212967
  }, {
    "starttime" : 1520863398147,
    "id" : "app-20180312150318-0186",
    "name" : "IE_Traitement_3",
    "cores" : 1,
    "user" : "root",
    "memoryperslave" : 1024,
    "submitdate" : "Mon Mar 12 15:03:18 CET 2018",
    "state" : "FINISHED",
    "duration" : 6321
  }, {
    "starttime" : 1520863387941,
    "id" : "app-20180312150307-0185",
    "name" : "IE_Traitement_0A",
    "cores" : 1,
    "user" : "root",
    "memoryperslave" : 1024,
    "submitdate" : "Mon Mar 12 15:03:07 CET 2018",
    "state" : "FINISHED",
    "duration" : 149536
  } ]}"""

import json
data = json.loads(req_json)

print(sorted(data['completedapps'], key=lambda x: x['starttime'])[0]['id'])

out:

app-20180312145939-0183

说明:首先获取dict列表,然后按时间戳排序。