[英]Creating a list from a set of values in a heavily nested dictionary
I'm trying to create a list out of the timestamps in this nested dictionary, but I'm having difficulty.我正在尝试从这个嵌套字典中的时间戳创建一个列表,但我遇到了困难。
{
'_class': 'org.jenkinsci.plugins.workflow.job.WorkflowJob',
'builds': [
{
'_class': 'org.jenkinsci.plugins.workflow.job.WorkflowRun',
'timestamp': 1571731200315
},
{
'_class': 'org.jenkinsci.plugins.workflow.job.WorkflowRun',
'timestamp': 1571731020310
},
{
'_class': 'org.jenkinsci.plugins.workflow.job.WorkflowRun',
'timestamp': 1571730995706
}, {
'_class': 'org.jenkinsci.plugins.workflow.job.WorkflowRun',
'timestamp': 1571730900316
}, {
'_class': 'org.jenkinsci.plugins.workflow.job.WorkflowRun',
'timestamp': 1571730600313
}
]
}
I've tried the following, but as the second value itself consists of a nested dictionary, it's not parsing the timestamps as I would like:我尝试了以下方法,但由于第二个值本身包含一个嵌套字典,它不会像我想要的那样解析时间戳:
dictList=[]
for key, value in dict.items():
dictList.append([value])
print (dictList[1])
The output from this attempt is the following:这次尝试的 output 如下:
[
[
{
'_class': 'org.jenkinsci.plugins.workflow.job.WorkflowRun',
'timestamp': 1571731200315
},
{
'_class': 'org.jenkinsci.plugins.workflow.job.WorkflowRun',
'timestamp': 1571731020310
},
{
'_class': 'org.jenkinsci.plugins.workflow.job.WorkflowRun',
'timestamp': 1571730995706
},
{
'_class': 'org.jenkinsci.plugins.workflow.job.WorkflowRun',
'timestamp': 1571730900316
},
{
'_class': 'org.jenkinsci.plugins.workflow.job.WorkflowRun',
'timestamp': 1571730600313
}
]
]
How can I drill down into the nested contents and retrieve only the timestamps?如何深入了解嵌套内容并仅检索时间戳?
dict = {'_class': 'org.jenkinsci.plugins.workflow.job.WorkflowJob', 'builds': [{'_class': 'org.jenkinsci.plugins.workflow.job.WorkflowRun', 'timestamp': 1571731200315}, {'_class': 'org.jenkinsci.plugins.workflow.job.WorkflowRun', 'timestamp': 1571731020310}, {'_class': 'org.jenkinsci.plugins.workflow.job.WorkflowRun', 'timestamp': 1571730995706}, {'_class': 'org.jenkinsci.plugins.workflow.job.WorkflowRun', 'timestamp': 1571730900316}, {'_class': 'org.jenkinsci.plugins.workflow.job.WorkflowRun', 'timestamp': 1571730600313}]}
timestamps = []
for i in dict['builds']:
timestamps.append(i['timestamp'])
print(timestamps)
I am not clear on this is what you want, but this can get all the timestamps into a list.我不清楚这是否是您想要的,但这可以将所有时间戳放入一个列表中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.