Python字典';s值是另一本字典——最好的方法?

Python字典';s值是另一本字典——最好的方法?,python,amazon-web-services,boto,Python,Amazon Web Services,Boto,我正在尝试创建一个字典,每个字典的值都将是另一个字典。这就是我正在做的 import json import boto.ec2.cloudwatch conn = boto.ec2.cloudwatch.connect_to_region('ap-southeast-1') alarms = conn.describe_alarms() all_dict = {} whitelist = ["name", "metric", "namespace", "statistic", "compar

我正在尝试创建一个字典,每个字典的值都将是另一个字典。这就是我正在做的

import json
import boto.ec2.cloudwatch

conn = boto.ec2.cloudwatch.connect_to_region('ap-southeast-1')
alarms = conn.describe_alarms()

all_dict = {}
whitelist = ["name", "metric", "namespace", "statistic", "comparison", "threshold", "period", "evaluation_periods", "unit", "description", "dimensions", "alarm_actions", "insufficient_data_actions", "ok_actions"]
i = 1
x = []
for alarm in alarms:
    single_dict = {}
    for attr in whitelist:
        single_dict[attr] = getattr(alarm, attr)
    all_dict[i] =  single_dict
    i = i+1
print json.dumps(all_dict, sort_keys=True, indent=4, separators=(',', ': '))

它工作得很好,但我觉得可能有一种比我所做的更聪明的方法。

如果你用
len(全部内容)
代替
I
,它看起来可能会更整洁一些。另外,它可能与上下文无关,但我不确定
x
在做什么


不过,如果代码有效,任何更改都纯粹是为了您的教育或美学。在内存和处理消耗方面的任何变化都可以忽略不计。

这个问题可能更适合;您有正在运行的代码,并要求改进。然而,在这里,让字典键入递增整数而不仅仅是一个列表似乎没有什么意义。@jonrsharpe我可以有一个列表,但如果我创建列表的JSON转储,我不确定如何迭代它。@yasra002在哪里迭代它?您可以加载JSON并取回列表。我如何才能做到这一点(加载JSON并取回列表)?有没有可能推荐一个关于python的链接或一本我可以阅读的书,其中有一些例子。