访问列表中字典的元素
accessing elements of a dictionary within a list
我有一本像
这样的字典
elements = {
"pipelineDescriptionList": [
{
"pipelineId": "df-09694461I15A2UPH0RZT",
"name": "EQUIPMENT_DEV",
"fields": [
{
"key": "@lastActivationTime",
"stringValue": "2020-12-09T05:30:21"
},
{
"key": "@nextRunTime",
"stringValue": "2021-04-12T00:10:00"
},
{
"key": "@creationTime",
"stringValue": "2020-12-09T05:26:06"
},
{
"key": "@sphere",
"stringValue": "PIPELINE"
},
{
"key": "@healthStatusUpdatedTime",
"stringValue": "2021-04-11T00:15:22"
},
{
"key": "@scheduledStartTime",
"stringValue": "2020-12-09T00:10:00"
},
{
"key": "@healthStatus",
"stringValue": "HEALTHY"
},
{
"key": "@latestRunTime",
"stringValue": "2021-04-11T00:10:00"
},
{
"key": "@version",
"stringValue": "1"
},
{
"key": "name",
"stringValue": "EQUIPMENT_DEV"
},
{
"key": "@id",
"stringValue": "df-09694461I15A2UPH0RZT"
},
{
"key": "@pipelineState",
"stringValue": "SCHEDULED"
},
{
"key": "uniqueId",
"stringValue": "96AEF597-33FD-420A-8D39-D328EBE0EC1C"
},
{
"key": "@scheduledPeriod",
"stringValue": "1 day"
},
{
"key": "@firstActivationTime",
"stringValue": "2020-12-09T05:30:21"
}
],
"tags": []
}
]
}
我需要访问键@pipelineState、@nextRunTime、@lastActivationTime 的字符串值。
我目前正在通过
访问它们
elements['pipelineDescriptionList'][0]['fields'][0]['stringValue']
elements['pipelineDescriptionList'][0]['fields'][1]['stringValue']
elements['pipelineDescriptionList'][0]['fields'][10]['stringValue']
但是我的管道字典不断变化,相同键的索引值对于不同的管道会有所不同。
例如:
elements = {
"pipelineDescriptionList": [
{
"pipelineId": "df-0440555DDFN1Z0N4UOR",
"name": "API_TOKEN_DEV",
"fields": [
{
"key": "@creationTime",
"stringValue": "2020-09-04T02:11:20"
},
{
"key": "@cancelActive",
"stringValue": "true"
},
{
"key": "@startTimestamp",
"stringValue": "2020-09-08T14:31:29"
},
{
"key": "pipelineCreator",
"stringValue": "AROAU2XMJTS2T67LQAZTF:kiranv@bizcloudexperts.com"
},
{
"key": "@version",
"stringValue": "2"
},
{
"key": "@id",
"stringValue": "df-0440555DDFN1Z0N4UOR"
},
{
"key": "@lastActivationTime",
"stringValue": "2020-09-08T14:31:29"
},
{
"key": "@nextRunTime",
"stringValue": "2021-04-12T02:11:00"
},
{
"key": "@lastDeactivationRequestTime",
"stringValue": "2020-09-05T02:09:29"
},
{
"key": "@sphere",
"stringValue": "PIPELINE"
},
{
"key": "@healthStatusUpdatedTime",
"stringValue": "2021-04-11T02:16:25"
},
{
"key": "@scheduledStartTime",
"stringValue": "2020-09-04T02:11:00"
},
{
"key": "@healthStatus",
"stringValue": "HEALTHY"
},
{
"key": "@latestRunTime",
"stringValue": "2021-04-11T02:11:00"
},
{
"key": "name",
"stringValue": "API_TOKEN_DEV"
},
{
"key": "@lastDeactivationTime",
"stringValue": "2020-09-05T02:11:29"
},
{
"key": "@pipelineState",
"stringValue": "SCHEDULED"
},
{
"key": "@firstActivationTime",
"stringValue": "2020-09-04T02:13:33"
}
]}
]
}
如何在不使用索引的情况下访问所需键的字符串值?有什么方法可以直接使用键名来获取 stringValues 吗?
看来你的主要问题是结构设计不好。如果您想按键访问值,请更改您的结构以完全做到这一点:
elements = {
"pipelineDescriptionList": [
{
"pipelineId": "df-09694461I15A2UPH0RZT",
"name": "EQUIPMENT_DEV",
"@lastActivationTime": "2020-12-09T05:30:21",
"@nextRunTime": "2021-04-12T00:10:00",
...
这会去掉无聊的数字索引,让您以更自然的方式访问所需的信息。
任何解决方案都需要您搜索(即遍历所有可能的字段)您想要的解决方案。因此,您可能应该通过更新字段来消除该步骤。
for element in elements["pipelineDescriptionList"]:
element['fields'] = {d['key']:d['stringValue'] for d in element['fields']}
现在您可以访问以下项目:
elements['pipelineDescriptionList'][0]['fields']['@nextRunTime']
我有一本像
这样的字典elements = {
"pipelineDescriptionList": [
{
"pipelineId": "df-09694461I15A2UPH0RZT",
"name": "EQUIPMENT_DEV",
"fields": [
{
"key": "@lastActivationTime",
"stringValue": "2020-12-09T05:30:21"
},
{
"key": "@nextRunTime",
"stringValue": "2021-04-12T00:10:00"
},
{
"key": "@creationTime",
"stringValue": "2020-12-09T05:26:06"
},
{
"key": "@sphere",
"stringValue": "PIPELINE"
},
{
"key": "@healthStatusUpdatedTime",
"stringValue": "2021-04-11T00:15:22"
},
{
"key": "@scheduledStartTime",
"stringValue": "2020-12-09T00:10:00"
},
{
"key": "@healthStatus",
"stringValue": "HEALTHY"
},
{
"key": "@latestRunTime",
"stringValue": "2021-04-11T00:10:00"
},
{
"key": "@version",
"stringValue": "1"
},
{
"key": "name",
"stringValue": "EQUIPMENT_DEV"
},
{
"key": "@id",
"stringValue": "df-09694461I15A2UPH0RZT"
},
{
"key": "@pipelineState",
"stringValue": "SCHEDULED"
},
{
"key": "uniqueId",
"stringValue": "96AEF597-33FD-420A-8D39-D328EBE0EC1C"
},
{
"key": "@scheduledPeriod",
"stringValue": "1 day"
},
{
"key": "@firstActivationTime",
"stringValue": "2020-12-09T05:30:21"
}
],
"tags": []
}
]
}
我需要访问键@pipelineState、@nextRunTime、@lastActivationTime 的字符串值。 我目前正在通过
访问它们elements['pipelineDescriptionList'][0]['fields'][0]['stringValue']
elements['pipelineDescriptionList'][0]['fields'][1]['stringValue']
elements['pipelineDescriptionList'][0]['fields'][10]['stringValue']
但是我的管道字典不断变化,相同键的索引值对于不同的管道会有所不同。 例如:
elements = {
"pipelineDescriptionList": [
{
"pipelineId": "df-0440555DDFN1Z0N4UOR",
"name": "API_TOKEN_DEV",
"fields": [
{
"key": "@creationTime",
"stringValue": "2020-09-04T02:11:20"
},
{
"key": "@cancelActive",
"stringValue": "true"
},
{
"key": "@startTimestamp",
"stringValue": "2020-09-08T14:31:29"
},
{
"key": "pipelineCreator",
"stringValue": "AROAU2XMJTS2T67LQAZTF:kiranv@bizcloudexperts.com"
},
{
"key": "@version",
"stringValue": "2"
},
{
"key": "@id",
"stringValue": "df-0440555DDFN1Z0N4UOR"
},
{
"key": "@lastActivationTime",
"stringValue": "2020-09-08T14:31:29"
},
{
"key": "@nextRunTime",
"stringValue": "2021-04-12T02:11:00"
},
{
"key": "@lastDeactivationRequestTime",
"stringValue": "2020-09-05T02:09:29"
},
{
"key": "@sphere",
"stringValue": "PIPELINE"
},
{
"key": "@healthStatusUpdatedTime",
"stringValue": "2021-04-11T02:16:25"
},
{
"key": "@scheduledStartTime",
"stringValue": "2020-09-04T02:11:00"
},
{
"key": "@healthStatus",
"stringValue": "HEALTHY"
},
{
"key": "@latestRunTime",
"stringValue": "2021-04-11T02:11:00"
},
{
"key": "name",
"stringValue": "API_TOKEN_DEV"
},
{
"key": "@lastDeactivationTime",
"stringValue": "2020-09-05T02:11:29"
},
{
"key": "@pipelineState",
"stringValue": "SCHEDULED"
},
{
"key": "@firstActivationTime",
"stringValue": "2020-09-04T02:13:33"
}
]}
]
}
如何在不使用索引的情况下访问所需键的字符串值?有什么方法可以直接使用键名来获取 stringValues 吗?
看来你的主要问题是结构设计不好。如果您想按键访问值,请更改您的结构以完全做到这一点:
elements = {
"pipelineDescriptionList": [
{
"pipelineId": "df-09694461I15A2UPH0RZT",
"name": "EQUIPMENT_DEV",
"@lastActivationTime": "2020-12-09T05:30:21",
"@nextRunTime": "2021-04-12T00:10:00",
...
这会去掉无聊的数字索引,让您以更自然的方式访问所需的信息。
任何解决方案都需要您搜索(即遍历所有可能的字段)您想要的解决方案。因此,您可能应该通过更新字段来消除该步骤。
for element in elements["pipelineDescriptionList"]:
element['fields'] = {d['key']:d['stringValue'] for d in element['fields']}
现在您可以访问以下项目:
elements['pipelineDescriptionList'][0]['fields']['@nextRunTime']