【发布时间】:2019-09-22 15:42:06
【问题描述】:
遍历字典结果列表错误:
AttributeError: 'list' 对象没有属性 'items'
变化:
for the_key, the_value in bucket.items():
到:
for the_key, the_value in bucket[0].items():
结果是第一个元素。我想捕获所有元素
bucket = [{'Name:': 'Share-1', 'Region': 'ap-south-1'}, {'Name:': 'Share-2', 'Region': 'us-west-1'}]
for the_key, the_value in bucket.items():
print(the_key, 'corresponds to', the_value)
实际结果:
AttributeError: 'list' 对象没有属性 'items'
想要的输出:
Name: Share-1
Region: ap-south-1
Name: Share-2
Region: us-west-1
【问题讨论】:
标签: python python-3.x list loops dictionary