Incrementing Python Dictionary Value based on a Counter -
i have dictionary 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 count how many of each there are.
decaadd_occurrences = {'2_506':3, '2_600':2, '1_650':1}
i created new dictionary of values updated.
deca_double_dict = {key: value 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 case, it's original dict without last item)
i'm trying figure out how increment num, values of counter_dict minus 1. update values except one, can stay same. the goal output allows 1 of duplicates keep same value, whereas rest have first number of value string incremented increasingly (based on number of duplicated counted). i'm trying achieve unique values data represented original deca_dict.
goal output = {'1':'3_506', '2':'4_506', '3':'2_506', '4':'3_600', '5':'2_600'}
i started going things following way, ended incrementing double items, resulting in had originally, except values plus one. for context: values of original deca_dict found concatenating 2 numbers (deca_address_num , deca_num_route). also, homeslayer qgis vector layer deca_address_num , deca_num_route stocked in fields indices d_address_idx , id_route_idx.
for key in deca_double_dict.keys(): 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 useless:
update_deca_dbl_dict = { "1": "3_506", "3": "3_506", "2": "3_506", "5": "3_600", "4": "3_600" }
my second try attempts include counter, things in wrong place.
for key, value in deca_double_dict.iteritems(): iterations = decaadd_occurrences[value] - 1 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 though 1 of answers below works incrementing duplicate items of dictionary, trying re-work code, need have comparison condition original data in order increment. still have same result first try (the useless one).
for key, value in deca_double_dict.iteritems(): 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
i think want. modified input dictionary better illustrate happens. primary difference doing decaadd_occurrences
, created counter
dictionary, keeps track of not counts, value of current address num
prefix. makes possible know next num
value use since both , count updated during process of modifying deca_dict
.
from collections import counter deca_dict = { "1": "2_506", "2": "2_506", "3": "2_506", "4": "2_600", "5": "1_650", "6": "2_600" } decaadd_occurrences = {k: (int(k.split('_')[0]), v) k,v in counter(deca_dict.values()).items()} key, value in deca_dict.items(): num, cnt = decaadd_occurrences[value] if cnt > 1: route = value.split('_')[1] next_num = num + 1 deca_dict[key] = '{}_{}'.format(next_num, route) decaadd_occurrences[value] = next_num, cnt-1 # update values
updated dictionary:
deca_dict -> { "1": "3_506", "2": "2_506", "3": "4_506", "4": "3_600", "5": "1_650", "6": "2_600" }
Comments
Post a Comment