首页 > 解决方案 > 如何在 boto3 python2.7 list dict 对象中搜索值 - list_tags_for_resource

问题描述

我在下面有响应对象

response = source.list_tags_for_resource(ResourceName=<ARN>)
    taglistk = response['TagList']
    print(taglistk)

输出 :

[{'Value': 'yes', 'Key': 'az'},{'Value': 'dba', 'Key': 'created'},{'Value': 'mariadb', 'Key': 'service'}]

现在我想写一个 for if 条件来匹配 K,V az=yes 和 service=mariadb 并做一些事情

标签: python-2.7boto3

解决方案


从响应的标签列表中,通过for循环选择一个标签并检查给定键的值,即Key和Value。

response = source.list_tags_for_resource(ResourceName=<ARN>)

for item in response:
    if item['Key'] == 'az' and item['Value'] == 'yes':
        # do something with item
    elif item['Key'] == 'service' and item['Value'] == 'mariadb':
        # do otherthing with item
    else:
        continue

推荐阅读