Hello there,
need an explanation and help how to write a script.
I'm fetching data from OpenStack cinder volumes API. The result is a dictionary with rows of dictionaries.
Every row has specific couple objects which values can be duplicated in another row of the main dictionary.
for example:
{id:gf23gf, size:5}
{id:iu38gr, size:5}
{id:gf23gf, size:5}
{id:iu38gr, size:5}
{id:gf23gf, size:5}
{id:gf23gf, size:5}
My goal is to get these values to a new dictionary. For the same "id" values I need to sum "size" values and result must be as one dictionary row:
{gf23gf:20, iu38gr:10}
my script:
api = [c for c in cinder.volumes.list(search_opts={'all_tenants':1})]
new_dict = {}
for i in api:
....api_dict = {} #rows with dictionaries where is all api's data
........for k,v in api_dict_.iteritems():
............api_dict[k] = v
........id = api_dict['id']
........size = api_dict['size']
........new_dict[id] = size
........if id in new_dict.keys():
............new_dict[id] += size
as an output I have new_dict{}, but the summarization goes incorrect.
Thanks in advance.
need an explanation and help how to write a script.
I'm fetching data from OpenStack cinder volumes API. The result is a dictionary with rows of dictionaries.
Every row has specific couple objects which values can be duplicated in another row of the main dictionary.
for example:
{id:gf23gf, size:5}
{id:iu38gr, size:5}
{id:gf23gf, size:5}
{id:iu38gr, size:5}
{id:gf23gf, size:5}
{id:gf23gf, size:5}
My goal is to get these values to a new dictionary. For the same "id" values I need to sum "size" values and result must be as one dictionary row:
{gf23gf:20, iu38gr:10}
my script:
api = [c for c in cinder.volumes.list(search_opts={'all_tenants':1})]
new_dict = {}
for i in api:
....api_dict = {} #rows with dictionaries where is all api's data
........for k,v in api_dict_.iteritems():
............api_dict[k] = v
........id = api_dict['id']
........size = api_dict['size']
........new_dict[id] = size
........if id in new_dict.keys():
............new_dict[id] += size
as an output I have new_dict{}, but the summarization goes incorrect.
Thanks in advance.