I have a dictionary with duplicate values.
Deca_dict = {
"1": "2_506",
"2": "2_506",
"3": "2_506",
"4": "2_600",
"5": "2_600",
"6": "1_650"
}
I have used collections.Counter to count how many of each there are.
decaAdd_occurrences = {'2_506':3, '2_600':2, '1_650':1}
I then created a new dictionary of values to be updated.
deca_double_dict = {key: value for key, value in Deca_dict.items()
if decaAdd_occurrences[value] > 1}
deca_double_dict = {
"1": "2_506",
"3": "2_506",
"2": "2_506",
"4": "2_600"
}
(in this case, it's the original dict without the last item)
I'm trying to figure out how to increment num, for the values of counter_dict minus 1. This will update all the values except one, which can stay the same. The goal output allows one of the duplicates to keep the same value, whereas the rest will have the first number of the value string incremented increasingly (based on the number of duplicated counted). I'm trying to achieve unique values for the data represented by the original Deca_dict.
Goal output = {'1':'3_506', '2':'4_506', '3':'2_506', '4':'3_600', '5':'2_600'}
I started going about things the following way, but ended up just incrementing all double items, resulting in what I had originally, except with values plus one. For context: The values of the original Deca_dict were found concatenating two numbers (deca_address_num and deca_num_route). Also, homesLayer is a QGIS vector layer where deca_address_num and deca_num_route are stocked in fields with indices d_address_idx and id_route_idx.
for key in deca_double_dict.keys():
for home in homesLayer.getFeatures():
if home.id() == key:
deca_address_num = home.attributes()[d_address_idx]
deca_num_route = home.attributes()[id_route_idx]
deca_address_plus = deca_address_num + increment
next_deca_address = (str(deca_address_plus) + '_' +
str(deca_num_route))
if not next_deca_address in Deca_dict.values():
update_deca_dbl_dict[key] = next_deca_address
The result is useless:
Update_deca_dbl_dict = {
"1": "3_506",
"3": "3_506",
"2": "3_506",
"5": "3_600",
"4": "3_600"
}
My second try attempts to include a counter, but things are in the wrong place.
for key, value in deca_double_dict.iteritems():
iterations = decaAdd_occurrences[value] - 1
for home in homesLayer.getFeatures():
if home.id() == key:
#deca_homeID_list.append(home.id())
increment = 1
deca_address_num = home.attributes()[d_address_idx]
deca_num_route = home.attributes()[id_route_idx]
deca_address_plus = deca_address_num + increment
next_deca_address = (str(deca_address_plus) + '_' +
str(deca_num_route))
#print deca_num_route
while iterations > 0:
if not next_deca_address in Deca_dict.values():
update_deca_dbl_dict[key] = next_deca_address
iterations -= 1
increment += 1
UPDATE Even though one of the answers below works for incrementing all duplicate items of my dictionary, I am trying to re-work my code, as I need to have this comparison condition to the original data in order to increment. I still have the same result as my first try (the useless one).
for key, value in deca_double_dict.iteritems():
for home in homesLayer.getFeatures():
if home.id() == key:
iterations = decaAdd_occurrences[value] - 1
increment = 1
while iterations > 0:
deca_address_num = home.attributes()[d_address_idx]
deca_num_route = home.attributes()[id_route_idx]
deca_address_plus = deca_address_num + increment
current_address = str(deca_address_num) + '_' + str(deca_num_route)
next_deca_address = (str(deca_address_plus) + '_' +
str(deca_num_route))
if not next_deca_address in Deca_dict.values():
update_deca_dbl_dict[key] = next_deca_address
iterations -= 1
increment += 1
else:
alpha_deca_dbl_dict[key] = current_address
iterations = 0