Consider I have Dictionary named "tenant_id_dict" with entry as follows:
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.
Now I need to utilize the tenant_id and instance_name as needed.
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.
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
. This list
is iterable and value for each key. So you need to iterate over the values (ie list
) as below.
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]})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.