我有一个从调用返回的 json 对象,类似于:
{
'Tags': [
{
'Key': 'Dept',
'PropagateAtLaunch': True,
'ResourceId': 'my-auto-scaling-group',
'ResourceType': 'auto-scaling-group',
'Value': 'Research',
},
{
'Key': 'Role',
'PropagateAtLaunch': True,
'ResourceId': 'my-auto-scaling-group',
'ResourceType': 'auto-scaling-group',
'Value': 'WebServer',
},
{
'Key': 'ecs_scaling',
'PropagateAtLaunch': True,
'ResourceId': 'my-auto-scaling-group',
'ResourceType': 'auto-scaling-group',
'Value': 'true',
},
],
'ResponseMetadata': {
'...': '...',
},
}
除了标准之外,是否有更 Pythonic 的方法来简单地确定 key ecs_scaling
是否存在:
data = json.loads(theThing)
for key in data.items():
...
key 可以是第一项,也可以是第 40 项——理想情况下,我希望能够执行类似 if 'ecs_scaling' in theKeys: ...
的操作?
最佳答案
您可以使用内置的 any
.
Return True if any element of the iterable is true. If the iterable is empty, return False.
if any(tag['key'] == 'ecs_scaling' for tag in data['Tags']):
...
关于确定 json 对象是否包含特定值的 Pythonic 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45964144/