[英]Processing of Items from a list
Consider I have Dictionary named "tenant_id_dict" with entry as follows: 考虑我有名为“ tenant_id_dict”的字典,其条目如下:
defaultdict(<type 'list'>, {u'b0116ce25cad4106becbbddfffa61a1c': [u'demo_ins1'], u'1578f81703ec4bbaa1d548532c922ab9': [u'new_ins_1', u'new_tenant_ins']})
That is combination of tenant_id and instance_name. 那是tenant_id和instance_name的组合。
Now I need to utilize the tenant_id and instance_name as needed. 现在,我需要根据需要使用tenant_id和instance_name。
Consider the following code: 考虑以下代码:
for tenants,instances in tenant_id_dict.iteritems():
compute_value_for_instance = ck.reports.get_total(tenant_id=tenants, service='compute', instance_name=instances)
print compute_value_for_instance
Here I need to process the values for tenants and instance_name dynamically. 在这里,我需要动态处理租户和instance_name的值。
But In above Code I am unable to get the result correctly as Instance is having the list of values. 但是在上面的代码中,由于实例具有值列表,因此我无法正确获取结果。
I need to process till the last item in a list. 我需要处理到列表中的最后一项。
Someone let me know the way to the processing of values here. 有人让我知道这里处理值的方式。
You initialized a defaultdit
with list
. 您使用
list
初始化了defaultdit
。 This list
is iterable and value for each key. 该
list
是可迭代的,并且是每个键的值。 So you need to iterate over the values (ie list
) as below. 因此,您需要如下迭代这些值(即
list
)。
for tenants,instances in tenant_id_dict.iteritems():
for single_ins in instances:
compute_value_for_instance = ck.reports.get_total(tenant_id=tenants, service='compute', instance_name=single_ins)
print compute_value_for_instance
Working Demo- 工作演示
from collections import defaultdict
#Create a dummy defaultdict with list values
d = defaultdict(list)
for i in range(5):
d[i].append(i+100)
d[i].append(i+200)
d[i].append(i+300)
#Experiment
for k,v in d.iteritems():
for single_vale in v:#Here v is a list
print str(k)+str(single_vale)
print d
Output- 输出 -
0100
0200
0300
1101
1201
1301
2102
2202
2302
3103
3203
3303
4104
4204
4304
defaultdict(<type 'list'>, {0: [100, 200, 300], 1: [101, 201, 301], 2: [102, 202, 302], 3: [103, 203, 303], 4: [104, 204, 304]})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.