below I have 2 list of dics, I would like to know how I can loop through both lists and find matching environments and ids and add the costs to my formatted_list where id == id and environment == environment. I only want a prod and non prod pair of id's in my formatted_list. My current logic is creating multiple id's in my formatted list. How can I just add costs to my formatted_ist from my results_ecs list?
def main(formatted_results_ecs, formatted_list):
ecs_deduction = 0
for ecs_cost in formatted_results_ecs:
for cost_est in formatted_list:
if ecs_cost["id"] == cost_est["id"] and ecs_cost["environment"] == cost_est["environment"]:
combined_cost = ecs_cost["cost"] cost_est["cost"]
cost_est["cost"] = combined_cost
ecs_deduction = combined_cost
if __name__ == '__main__':
formatted_results_ecs = [
{'id': 'ffd07d03-e10b-4d60-8e28-e4ab617bf394', 'cost': 3.2980019999999994, 'environment': 'prod'},
{'id': 'ffc87314-16e4-4c77-b5ec-f34066458ce4', 'cost': 215.82577600000002, 'environment': 'prod'},
{'id': 'ff66d353-6644-4c61-9c22-204960d65a18', 'cost': 291.61807799999997, 'environment': 'prod'},
{'id': 'a855d85f-65e4-40bb-a21f-6ceea77628cc', 'cost': 19.041902000000004, 'environment': 'prod'},
{'id': 'aa385029-afa6-4f1a-a1d9-d88b7d934699', 'cost': 2.033628000000001, 'environment': 'prod'},
{'id': 'adf44794-50eb-45db-9323-6ce5fd31699e', 'cost': 1120.8391390000004, 'environment': 'prod'},
{'id': 'b13a0676-6926-49db-808c-3c968a9278eb', 'cost': 15145.140081999996, 'environment': 'prod'},
{'id': 'e4c3c7b9-5941-4cf7-aa90-f63a142ccaf4', 'cost': 3319.4728399999995, 'environment': 'prod'},
{'id': 'ff66d353-6644-4c61-9c22-204960d65a18', 'cost': 6.0234558329471, 'environment': 'nonprod'},
{'id': 'ff66d353-6644-4c61-9c22-204960d65a18', 'cost': 7.9407999472248, 'environment': 'prod'},
{'id': '2d1e7f15-d3bb-4649-996e-a7afb55b7637', 'cost': 5.940205, 'environment': 'prod'},
{'id': '6841509d-1d68-4af3-b04e-8bc7e87d0c89', 'cost': 651.016164, 'environment': 'prod'},
{'id': 'ffd07d03-e10b-4d60-8e28-e4ab617bf394', 'cost': 3.287444999999999, 'environment': 'nonprod'},
{'id': 'ffc87314-16e4-4c77-b5ec-f34066458ce4', 'cost': 214.46528800000002, 'environment': 'nonprod'},
{'id': 'ff66d353-6644-4c61-9c22-204960d65a18', 'cost': 291.55481199999997, 'environment': 'nonprod'},
{'id': 'aa385029-afa6-4f1a-a1d9-d88b7d934699', 'cost': 2.0193450000000013, 'environment': 'nonprod'},
]
formatted_list = [
{'id': 'ffd07d03-e10b-4d60-8e28-e4ab617bf394', 'cost': 13.431203414269257, 'environment': 'nonprod'},
{'id': 'ffc87314-16e4-4c77-b5ec-f34066458ce4', 'cost': 3.1723085934801296, 'environment': 'nonprod'},
{'id': 'ff66d353-6644-4c61-9c22-204960d65a18', 'cost': 602.0234558329471, 'environment': 'nonprod'},
{'id': 'ff66d353-6644-4c61-9c22-204960d65a18', 'cost': 607.9407999472248, 'environment': 'prod'},
{'id': 'ff4f589f-2616-4f5e-9fb0-09e7ab3d200c', 'cost': 0.187242731922548, 'environment': 'prod'},
{'id': 'ff2041fb-711f-4bca-a336-5c6ac07fb8f3', 'cost': 1.179409983e-06, 'environment': 'nonprod'},
{'id': 'ff2041fb-711f-4bca-a336-5c6ac07fb8f3', 'cost': 1.179409983e-06, 'environment': 'prod'},
{'id': 'fba58f4f-303c-4a20-86d3-294714c8db5c', 'cost': 14.494946621907866, 'environment': 'nonprod'},
{'id': 'f8780ef9-0211-486b-ac2d-1b85d6bcb9da', 'cost': 19.592325435434688, 'environment': 'nonprod'},
{'id': 'f8780ef9-0211-486b-ac2d-1b85d6bcb9da', 'cost': 19.288246392238705, 'environment': 'prod'},
{'id': 'f7f16766-1212-4747-9c98-7a37d125e850', 'cost': 0.8798593169569822, 'environment': 'nonprod'},
{'id': 'f7d59e13-cba1-4a13-af57-8c4aec3ee281', 'cost': 30.22516723291206, 'environment': 'prod'},
{'id': 'f7d59e13-cba1-4a13-af57-8c4aec3ee281', 'cost': 29.88632221088805, 'environment': 'nonprod'},
{'id': 'f742f6ea-01dc-4add-a283-6afa6656a87b', 'cost': 18.657917564221908, 'environment': 'prod'}
]
main(formatted_list, formatted_results_ecs)
a good example id is ff66d353-6644-4c61-9c22-204960d65a18. This id shows up 4 times in the formatted_result_ecs list and im trying to add cost by env to my foratted_list. Any breakdown or explanation of how this solution should work would be greatly appreciated.
CodePudding user response:
Here is what I described above. I mogrify the formatted list into a dictionary form, which makes the lookups easy. I mogrify it back to the original form after. In the Perl world, this is called a "Schwartzian transform".
from pprint import pprint
def main(formatted_results_ecs, formatted_list):
# Reformat formatted list.
fmt1 = {}
for row in formatted_list:
fmt1[(row['id'],row['environment'])] = row['cost']
for row in formatted_results_ecs:
key = (row['id'],row['environment'])
if key in fmt1:
fmt1[key] = row['cost']
else:
fmt1[key] = row['cost']
pprint(fmt1)
# return to original format.
fmtl = [{'id': k[0], 'cost': v, 'environment': k[1]} for k,v in fmt1.items()]
fmtl.sort(key=lambda k: k['id'])
pprint(fmtl)
if __name__ == '__main__':
formatted_results_ecs = [
{'id': 'ffd07d03-e10b-4d60-8e28-e4ab617bf394', 'cost': 3.2980019999999994, 'environment': 'prod'},
{'id': 'ffc87314-16e4-4c77-b5ec-f34066458ce4', 'cost': 215.82577600000002, 'environment': 'prod'},
{'id': 'ff66d353-6644-4c61-9c22-204960d65a18', 'cost': 291.61807799999997, 'environment': 'prod'},
{'id': 'a855d85f-65e4-40bb-a21f-6ceea77628cc', 'cost': 19.041902000000004, 'environment': 'prod'},
{'id': 'aa385029-afa6-4f1a-a1d9-d88b7d934699', 'cost': 2.033628000000001, 'environment': 'prod'},
{'id': 'adf44794-50eb-45db-9323-6ce5fd31699e', 'cost': 1120.8391390000004, 'environment': 'prod'},
{'id': 'b13a0676-6926-49db-808c-3c968a9278eb', 'cost': 15145.140081999996, 'environment': 'prod'},
{'id': 'e4c3c7b9-5941-4cf7-aa90-f63a142ccaf4', 'cost': 3319.4728399999995, 'environment': 'prod'},
{'id': 'ff66d353-6644-4c61-9c22-204960d65a18', 'cost': 6.0234558329471, 'environment': 'nonprod'},
{'id': 'ff66d353-6644-4c61-9c22-204960d65a18', 'cost': 7.9407999472248, 'environment': 'prod'},
{'id': '2d1e7f15-d3bb-4649-996e-a7afb55b7637', 'cost': 5.940205, 'environment': 'prod'},
{'id': '6841509d-1d68-4af3-b04e-8bc7e87d0c89', 'cost': 651.016164, 'environment': 'prod'},
{'id': 'ffd07d03-e10b-4d60-8e28-e4ab617bf394', 'cost': 3.287444999999999, 'environment': 'nonprod'},
{'id': 'ffc87314-16e4-4c77-b5ec-f34066458ce4', 'cost': 214.46528800000002, 'environment': 'nonprod'},
{'id': 'ff66d353-6644-4c61-9c22-204960d65a18', 'cost': 291.55481199999997, 'environment': 'nonprod'},
{'id': 'aa385029-afa6-4f1a-a1d9-d88b7d934699', 'cost': 2.0193450000000013, 'environment': 'nonprod'},
]
formatted_list = [
{'id': 'ffd07d03-e10b-4d60-8e28-e4ab617bf394', 'cost': 13.431203414269257, 'environment': 'nonprod'},
{'id': 'ffc87314-16e4-4c77-b5ec-f34066458ce4', 'cost': 3.1723085934801296, 'environment': 'nonprod'},
{'id': 'ff66d353-6644-4c61-9c22-204960d65a18', 'cost': 602.0234558329471, 'environment': 'nonprod'},
{'id': 'ff66d353-6644-4c61-9c22-204960d65a18', 'cost': 607.9407999472248, 'environment': 'prod'},
{'id': 'ff4f589f-2616-4f5e-9fb0-09e7ab3d200c', 'cost': 0.187242731922548, 'environment': 'prod'},
{'id': 'ff2041fb-711f-4bca-a336-5c6ac07fb8f3', 'cost': 1.179409983e-06, 'environment': 'nonprod'},
{'id': 'ff2041fb-711f-4bca-a336-5c6ac07fb8f3', 'cost': 1.179409983e-06, 'environment': 'prod'},
{'id': 'fba58f4f-303c-4a20-86d3-294714c8db5c', 'cost': 14.494946621907866, 'environment': 'nonprod'},
{'id': 'f8780ef9-0211-486b-ac2d-1b85d6bcb9da', 'cost': 19.592325435434688, 'environment': 'nonprod'},
{'id': 'f8780ef9-0211-486b-ac2d-1b85d6bcb9da', 'cost': 19.288246392238705, 'environment': 'prod'},
{'id': 'f7f16766-1212-4747-9c98-7a37d125e850', 'cost': 0.8798593169569822, 'environment': 'nonprod'},
{'id': 'f7d59e13-cba1-4a13-af57-8c4aec3ee281', 'cost': 30.22516723291206, 'environment': 'prod'},
{'id': 'f7d59e13-cba1-4a13-af57-8c4aec3ee281', 'cost': 29.88632221088805, 'environment': 'nonprod'},
{'id': 'f742f6ea-01dc-4add-a283-6afa6656a87b', 'cost': 18.657917564221908, 'environment': 'prod'}
]
main( formatted_results_ecs, formatted_list )
Output:
{('2d1e7f15-d3bb-4649-996e-a7afb55b7637', 'prod'): 5.940205,
('6841509d-1d68-4af3-b04e-8bc7e87d0c89', 'prod'): 651.016164,
('a855d85f-65e4-40bb-a21f-6ceea77628cc', 'prod'): 19.041902000000004,
('aa385029-afa6-4f1a-a1d9-d88b7d934699', 'nonprod'): 2.0193450000000013,
('aa385029-afa6-4f1a-a1d9-d88b7d934699', 'prod'): 2.033628000000001,
('adf44794-50eb-45db-9323-6ce5fd31699e', 'prod'): 1120.8391390000004,
('b13a0676-6926-49db-808c-3c968a9278eb', 'prod'): 15145.140081999996,
('e4c3c7b9-5941-4cf7-aa90-f63a142ccaf4', 'prod'): 3319.4728399999995,
('f742f6ea-01dc-4add-a283-6afa6656a87b', 'prod'): 18.657917564221908,
('f7d59e13-cba1-4a13-af57-8c4aec3ee281', 'nonprod'): 29.88632221088805,
('f7d59e13-cba1-4a13-af57-8c4aec3ee281', 'prod'): 30.22516723291206,
('f7f16766-1212-4747-9c98-7a37d125e850', 'nonprod'): 0.8798593169569822,
('f8780ef9-0211-486b-ac2d-1b85d6bcb9da', 'nonprod'): 19.592325435434688,
('f8780ef9-0211-486b-ac2d-1b85d6bcb9da', 'prod'): 19.288246392238705,
('fba58f4f-303c-4a20-86d3-294714c8db5c', 'nonprod'): 14.494946621907866,
('ff2041fb-711f-4bca-a336-5c6ac07fb8f3', 'nonprod'): 1.179409983e-06,
('ff2041fb-711f-4bca-a336-5c6ac07fb8f3', 'prod'): 1.179409983e-06,
('ff4f589f-2616-4f5e-9fb0-09e7ab3d200c', 'prod'): 0.187242731922548,
('ff66d353-6644-4c61-9c22-204960d65a18', 'nonprod'): 899.6017236658942,
('ff66d353-6644-4c61-9c22-204960d65a18', 'prod'): 907.4996778944496,
('ffc87314-16e4-4c77-b5ec-f34066458ce4', 'nonprod'): 217.63759659348014,
('ffc87314-16e4-4c77-b5ec-f34066458ce4', 'prod'): 215.82577600000002,
('ffd07d03-e10b-4d60-8e28-e4ab617bf394', 'nonprod'): 16.718648414269257,
('ffd07d03-e10b-4d60-8e28-e4ab617bf394', 'prod'): 3.2980019999999994}
[{'cost': 5.940205,
'environment': 'prod',
'id': '2d1e7f15-d3bb-4649-996e-a7afb55b7637'},
{'cost': 651.016164,
'environment': 'prod',
'id': '6841509d-1d68-4af3-b04e-8bc7e87d0c89'},
{'cost': 19.041902000000004,
'environment': 'prod',
'id': 'a855d85f-65e4-40bb-a21f-6ceea77628cc'},
{'cost': 2.033628000000001,
'environment': 'prod',
'id': 'aa385029-afa6-4f1a-a1d9-d88b7d934699'},
{'cost': 2.0193450000000013,
'environment': 'nonprod',
'id': 'aa385029-afa6-4f1a-a1d9-d88b7d934699'},
{'cost': 1120.8391390000004,
'environment': 'prod',
'id': 'adf44794-50eb-45db-9323-6ce5fd31699e'},
{'cost': 15145.140081999996,
'environment': 'prod',
'id': 'b13a0676-6926-49db-808c-3c968a9278eb'},
{'cost': 3319.4728399999995,
'environment': 'prod',
'id': 'e4c3c7b9-5941-4cf7-aa90-f63a142ccaf4'},
{'cost': 18.657917564221908,
'environment': 'prod',
'id': 'f742f6ea-01dc-4add-a283-6afa6656a87b'},
{'cost': 30.22516723291206,
'environment': 'prod',
'id': 'f7d59e13-cba1-4a13-af57-8c4aec3ee281'},
{'cost': 29.88632221088805,
'environment': 'nonprod',
'id': 'f7d59e13-cba1-4a13-af57-8c4aec3ee281'},
{'cost': 0.8798593169569822,
'environment': 'nonprod',
'id': 'f7f16766-1212-4747-9c98-7a37d125e850'},
{'cost': 19.592325435434688,
'environment': 'nonprod',
'id': 'f8780ef9-0211-486b-ac2d-1b85d6bcb9da'},
{'cost': 19.288246392238705,
'environment': 'prod',
'id': 'f8780ef9-0211-486b-ac2d-1b85d6bcb9da'},
{'cost': 14.494946621907866,
'environment': 'nonprod',
'id': 'fba58f4f-303c-4a20-86d3-294714c8db5c'},
{'cost': 1.179409983e-06,
'environment': 'nonprod',
'id': 'ff2041fb-711f-4bca-a336-5c6ac07fb8f3'},
{'cost': 1.179409983e-06,
'environment': 'prod',
'id': 'ff2041fb-711f-4bca-a336-5c6ac07fb8f3'},
{'cost': 0.187242731922548,
'environment': 'prod',
'id': 'ff4f589f-2616-4f5e-9fb0-09e7ab3d200c'},
{'cost': 899.6017236658942,
'environment': 'nonprod',
'id': 'ff66d353-6644-4c61-9c22-204960d65a18'},
{'cost': 907.4996778944496,
'environment': 'prod',
'id': 'ff66d353-6644-4c61-9c22-204960d65a18'},
{'cost': 217.63759659348014,
'environment': 'nonprod',
'id': 'ffc87314-16e4-4c77-b5ec-f34066458ce4'},
{'cost': 215.82577600000002,
'environment': 'prod',
'id': 'ffc87314-16e4-4c77-b5ec-f34066458ce4'},
{'cost': 16.718648414269257,
'environment': 'nonprod',
'id': 'ffd07d03-e10b-4d60-8e28-e4ab617bf394'},
{'cost': 3.2980019999999994,
'environment': 'prod',
'id': 'ffd07d03-e10b-4d60-8e28-e4ab617bf394'}]
CodePudding user response:
you can try using below statement ( you may need to adjust your code to your need) , but mering dictionary could be a better option
result_set= [
{'id': 'ffd07d03-e10b-4d60-8e28-e4ab617bf394', 'cost': 3.2980019999999994, 'environment': 'prod'},
{'id': 'ffc87314-16e4-4c77-b5ec-f34066458ce4', 'cost': 215.82577600000002, 'environment': 'prod'},
{'id': 'ff66d353-6644-4c61-9c22-204960d65a18', 'cost': 291.61807799999997, 'environment': 'prod'},
{'id': 'a855d85f-65e4-40bb-a21f-6ceea77628cc', 'cost': 19.041902000000004, 'environment': 'prod'},
{'id': 'aa385029-afa6-4f1a-a1d9-d88b7d934699', 'cost': 2.033628000000001, 'environment': 'prod'},
{'id': 'adf44794-50eb-45db-9323-6ce5fd31699e', 'cost': 1120.8391390000004, 'environment': 'prod'},
{'id': 'b13a0676-6926-49db-808c-3c968a9278eb', 'cost': 15145.140081999996, 'environment': 'prod'},
{'id': 'e4c3c7b9-5941-4cf7-aa90-f63a142ccaf4', 'cost': 3319.4728399999995, 'environment': 'prod'},
{'id': 'ff66d353-6644-4c61-9c22-204960d65a18', 'cost': 6.0234558329471, 'environment': 'nonprod'},
{'id': 'ff66d353-6644-4c61-9c22-204960d65a18', 'cost': 7.9407999472248, 'environment': 'prod'},
{'id': '2d1e7f15-d3bb-4649-996e-a7afb55b7637', 'cost': 5.940205, 'environment': 'prod'},
{'id': '6841509d-1d68-4af3-b04e-8bc7e87d0c89', 'cost': 651.016164, 'environment': 'prod'},
{'id': 'ffd07d03-e10b-4d60-8e28-e4ab617bf394', 'cost': 3.287444999999999, 'environment': 'nonprod'},
{'id': 'ffc87314-16e4-4c77-b5ec-f34066458ce4', 'cost': 214.46528800000002, 'environment': 'nonprod'},
{'id': 'ff66d353-6644-4c61-9c22-204960d65a18', 'cost': 291.55481199999997, 'environment': 'nonprod'},
{'id': 'aa385029-afa6-4f1a-a1d9-d88b7d934699', 'cost': 2.0193450000000013, 'environment': 'nonprod'},
]
print(result_set)
interim_list = [
{'id': 'ffd07d03-e10b-4d60-8e28-e4ab617bf394', 'cost': 13.431203414269257, 'environment': 'nonprod'},
{'id': 'ffc87314-16e4-4c77-b5ec-f34066458ce4', 'cost': 3.1723085934801296, 'environment': 'nonprod'},
{'id': 'ff66d353-6644-4c61-9c22-204960d65a18', 'cost': 602.0234558329471, 'environment': 'nonprod'},
{'id': 'ff66d353-6644-4c61-9c22-204960d65a18', 'cost': 607.9407999472248, 'environment': 'prod'},
{'id': 'ff4f589f-2616-4f5e-9fb0-09e7ab3d200c', 'cost': 0.187242731922548, 'environment': 'prod'},
{'id': 'ff2041fb-711f-4bca-a336-5c6ac07fb8f3', 'cost': 1.179409983e-06, 'environment': 'nonprod'},
{'id': 'ff2041fb-711f-4bca-a336-5c6ac07fb8f3', 'cost': 1.179409983e-06, 'environment': 'prod'},
{'id': 'fba58f4f-303c-4a20-86d3-294714c8db5c', 'cost': 14.494946621907866, 'environment': 'nonprod'},
{'id': 'f8780ef9-0211-486b-ac2d-1b85d6bcb9da', 'cost': 19.592325435434688, 'environment': 'nonprod'},
{'id': 'f8780ef9-0211-486b-ac2d-1b85d6bcb9da', 'cost': 19.288246392238705, 'environment': 'prod'},
{'id': 'f7f16766-1212-4747-9c98-7a37d125e850', 'cost': 0.8798593169569822, 'environment': 'nonprod'},
{'id': 'f7d59e13-cba1-4a13-af57-8c4aec3ee281', 'cost': 30.22516723291206, 'environment': 'prod'},
{'id': 'f7d59e13-cba1-4a13-af57-8c4aec3ee281', 'cost': 29.88632221088805, 'environment': 'nonprod'},
{'id': 'f742f6ea-01dc-4add-a283-6afa6656a87b', 'cost': 18.657917564221908, 'environment': 'prod'}
]
print(interim_list)
def merge_list_result(result_set, interim_list):
final_list = {**result_set, **interim_list}
for id, value in final_list.id():
if id in result_set & id in interim_list:
final_list[id] = [value, result_set[id]]
return final_list
final_list = merge_final_result(result_set, interim_list)
print(final_list)