I have a list of dictionaries that look like this:
[
{'ServiceID': 20, 'primary': '20', 'secondary': '12'},
{'ServiceID': 20, 'primary': '20', 'secondary': '12'},
{'ServiceID': 20, 'primary': '20', 'secondary': '12'},
{'ServiceID': 16, 'primary': '16', 'secondary': '8'},
{'ServiceID': 20, 'primary': '20', 'secondary': '12'},
{'ServiceID': 8, 'primary': '8', 'secondary': '16'},
{'ServiceID': 12, 'primary': '12', 'secondary': '20'},
{'ServiceID': 8, 'primary': '8', 'secondary': '16'}
]
I would like create a new sorted dictionary where the we have the following:
key = value of 'ServiceID'
key = value of how many times that particular 'ServiceID' is listed as a 'primary'
key = value of how many times that particular 'ServiceID' is listed as a 'secondary'
For example:
[
{'ServiceID': 8, 'primaryCount': 2, 'secondaryCount': 1},
{'ServiceID': 12, 'primaryCount': 1, 'secondaryCount': 4},
{'ServiceID': 16, 'primaryCount': 1, 'secondaryCount': 2},
{'ServiceID': 120, 'primaryCount': 4, 'secondaryCount': 1}
]
Code that I have so far that doesn't quite seem to do what I desire, meaning that I am unsure as to how to appropriately increment the number of primaries and secondaries across the entire for loop as well as how to only ensure I am capturing the uniques for the 'ServiceID'
I believe there is something wrong with my logic:
temp_count_list = list()
temp_primary_counts = 0
temp_secondary_counts = 0
for sub_dict in new_list:
temp_dict = dict()
temp_dict['ServiceID'] = sub_dict['ServiceID']
if sub_dict['ServiceID'] == int(sub_dict['primarySlice']):
temp_dict['primaryCount'] = temp_primary_counts +=1
if sub_dict['ServiceID'] == int(sub_dict['secondarySlice']):
temp_dict['secondaryCount'] = temp_secondary_counts +=1
temp_count_list.append(temp_dict)