gpt4 book ai didi

python - 如何从字典数组创建值列表?

转载 作者:行者123 更新时间:2023-12-01 07:51:28 26 4
gpt4 key购买 nike

我正在使用 Boto 编写 Python 脚本。当我进行描述时,我会得到一个包含 10 个项目的字典列表。这是示例输出:

[{
"arn": "arn:aws:health:us-east-1::event/EC2/AWS_EC2_PERSISTENT_INSTANCE_RETIREMENT_SCHEDULED/AWS_EC2_PERSISTENT_INSTANCE_RETIREMENT_SCHEDULED5ac93e9d-18fb-49a0-bd93-9b23c81f23bd",
"service": "EC2",
"eventTypeCode": "AWS_EC2_PERSISTENT_INSTANCE_RETIREMENT_SCHEDULED",
"eventTypeCategory": "scheduledChange",
"region": "us-east-1",
"startTime": "2019-05-31 13:00:00-04:00",
"endTime": "2019-05-31 13:00:00-04:00",
"lastUpdatedTime": "2019-05-17 13:29:54.458000-04:00",
"statusCode": "upcoming"
}, {
"arn": "arn:aws:health:us-east-1::event/EC2/AWS_EC2_PERSISTENT_INSTANCE_RETIREMENT_SCHEDULED/AWS_EC2_PERSISTENT_INSTANCE_RETIREMENT_SCHEDULED67080365-2d7d-4f63-8d74-ab67995b7f55",
"service": "EC2",
"eventTypeCode": "AWS_EC2_PERSISTENT_INSTANCE_RETIREMENT_SCHEDULED",
"eventTypeCategory": "scheduledChange",
"region": "us-east-1",
"startTime": "2019-05-31 13:00:00-04:00",
"endTime": "2019-05-31 13:00:00-04:00",
"lastUpdatedTime": "2019-05-17 13:29:50.932000-04:00",
"statusCode": "upcoming"
}, {
"arn": "arn:aws:health:us-east-1::event/EC2/AWS_EC2_PERSISTENT_INSTANCE_RETIREMENT_SCHEDULED/AWS_EC2_PERSISTENT_INSTANCE_RETIREMENT_SCHEDULED30e49b6a-0729-42aa-bcea-7a5f1d15368e",
"service": "EC2",
"eventTypeCode": "AWS_EC2_PERSISTENT_INSTANCE_RETIREMENT_SCHEDULED",
"eventTypeCategory": "scheduledChange",
"region": "us-east-1",
"startTime": "2019-05-31 13:00:00-04:00",
"endTime": "2019-05-31 13:00:00-04:00",
"lastUpdatedTime": "2019-05-17 13:29:47.118000-04:00",
"statusCode": "upcoming"
}, {
"arn": "arn:aws:health:us-east-1::event/EC2/AWS_EC2_PERSISTENT_INSTANCE_RETIREMENT_SCHEDULED/AWS_EC2_PERSISTENT_INSTANCE_RETIREMENT_SCHEDULED8226db8a-7d54-4495-882f-9f4e2a09757b",
"service": "EC2",
"eventTypeCode": "AWS_EC2_PERSISTENT_INSTANCE_RETIREMENT_SCHEDULED",
"eventTypeCategory": "scheduledChange",
"region": "us-east-1",
"startTime": "2019-05-31 13:00:00-04:00",
"endTime": "2019-05-31 13:00:00-04:00",
"lastUpdatedTime": "2019-05-17 13:29:43.011000-04:00",
"statusCode": "upcoming"
}, {
"arn": "arn:aws:health:us-east-1::event/EC2/AWS_EC2_PERSISTENT_INSTANCE_RETIREMENT_SCHEDULED/AWS_EC2_PERSISTENT_INSTANCE_RETIREMENT_SCHEDULED269202de-d110-4cc2-8a58-b60d830ff188",
"service": "EC2",
"eventTypeCode": "AWS_EC2_PERSISTENT_INSTANCE_RETIREMENT_SCHEDULED",
"eventTypeCategory": "scheduledChange",
"region": "us-east-1",
"startTime": "2019-05-31 13:00:00-04:00",
"endTime": "2019-05-31 13:00:00-04:00",
"lastUpdatedTime": "2019-05-17 13:29:38.433000-04:00",
"statusCode": "upcoming"
}, {
"arn": "arn:aws:health:us-east-1::event/EC2/AWS_EC2_PERSISTENT_INSTANCE_RETIREMENT_SCHEDULED/AWS_EC2_PERSISTENT_INSTANCE_RETIREMENT_SCHEDULED4d6677ff-8250-4d3a-bb06-ebf5efd59079",
"service": "EC2",
"eventTypeCode": "AWS_EC2_PERSISTENT_INSTANCE_RETIREMENT_SCHEDULED",
"eventTypeCategory": "scheduledChange",
"region": "us-east-1",
"startTime": "2019-05-31 13:00:00-04:00",
"endTime": "2019-05-31 13:00:00-04:00",
"lastUpdatedTime": "2019-05-17 13:29:34.988000-04:00",
"statusCode": "upcoming"
}, {
"arn": "arn:aws:health:us-east-1::event/EC2/AWS_EC2_PERSISTENT_INSTANCE_RETIREMENT_SCHEDULED/AWS_EC2_PERSISTENT_INSTANCE_RETIREMENT_SCHEDULED973f686a-f03f-4a28-a966-129e3f36d0f0",
"service": "EC2",
"eventTypeCode": "AWS_EC2_PERSISTENT_INSTANCE_RETIREMENT_SCHEDULED",
"eventTypeCategory": "scheduledChange",
"region": "us-east-1",
"startTime": "2019-05-31 13:00:00-04:00",
"endTime": "2019-05-31 13:00:00-04:00",
"lastUpdatedTime": "2019-05-17 13:29:32.072000-04:00",
"statusCode": "upcoming"
}, {
"arn": "arn:aws:health:us-east-1::event/EC2/AWS_EC2_PERSISTENT_INSTANCE_RETIREMENT_SCHEDULED/AWS_EC2_PERSISTENT_INSTANCE_RETIREMENT_SCHEDULED067b4edf-e984-4595-8d96-89e5cdcb82ee",
"service": "EC2",
"eventTypeCode": "AWS_EC2_PERSISTENT_INSTANCE_RETIREMENT_SCHEDULED",
"eventTypeCategory": "scheduledChange",
"region": "us-east-1",
"startTime": "2019-05-31 13:00:00-04:00",
"endTime": "2019-05-31 13:00:00-04:00",
"lastUpdatedTime": "2019-05-17 13:29:29.616000-04:00",
"statusCode": "upcoming"
}, {
"arn": "arn:aws:health:us-east-1::event/EC2/AWS_EC2_PERSISTENT_INSTANCE_RETIREMENT_SCHEDULED/AWS_EC2_PERSISTENT_INSTANCE_RETIREMENT_SCHEDULED21b61601-6342-4e23-b3c3-d4384eccdd61",
"service": "EC2",
"eventTypeCode": "AWS_EC2_PERSISTENT_INSTANCE_RETIREMENT_SCHEDULED",
"eventTypeCategory": "scheduledChange",
"region": "us-east-1",
"startTime": "2019-05-31 13:00:00-04:00",
"endTime": "2019-05-31 13:00:00-04:00",
"lastUpdatedTime": "2019-05-17 13:29:24.326000-04:00",
"statusCode": "upcoming"
}, {
"arn": "arn:aws:health:us-east-1::event/EC2/AWS_EC2_PERSISTENT_INSTANCE_RETIREMENT_SCHEDULED/AWS_EC2_PERSISTENT_INSTANCE_RETIREMENT_SCHEDULED13bf770d-9a7a-4434-be3d-9d4f1d162d66",
"service": "EC2",
"eventTypeCode": "AWS_EC2_PERSISTENT_INSTANCE_RETIREMENT_SCHEDULED",
"eventTypeCategory": "scheduledChange",
"region": "us-east-1",
"startTime": "2019-05-31 13:00:00-04:00",
"endTime": "2019-05-31 13:00:00-04:00",
"lastUpdatedTime": "2019-05-17 13:29:21.642000-04:00",
"statusCode": "upcoming"
}]

现在我想创建一个仅包含每个项目的 ARN 的列表。我可以循环遍历每个项目,例如:

for i in len(var1):
var2.append(var1[i]['arn']
print var2

但是有没有更好的方法来编写这个循环?

最佳答案

列表理解会最紧凑地完成它:

arns = [x['arn'] for x in data]

关于python - 如何从字典数组创建值列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56193435/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com