0

I just got something working, but realized I have a missing piece. I'm trying to get duplicate values in a dictionary, increment the numbers based on the number of duplicates (to leave one the same), but all based on an order (which is in reality a distance along a road).

Starting with a dictionary like this:

Deca_dict = {
    "1": "2_506",
    "2": "2_506",
    "3": "2_506",
    "4": "2_600",
    "5": "1_650",
    "6": "2_600"
}

With the help of an SO answer, I counted the number of occurrences and kept track of the original number i'm interested in incrementing:

decaAdd_occurrences = {k: (int(k.split('_')[0]), v) for k,v in
                                Counter(Deca_dict.values()).items()}

I adapted the answer's code like so:

changed_deca_dict = {}
alpha_deca_dict = {}
for key, value in Deca_dict.items():
    num, cnt = decaAdd_occurrences[value]
    if cnt > 1:
        route = value.split('_')[1]
        next_num = num + 1
        next_add = str(next_num) + '_' + str(route)
        if not next_add in Deca_dict.values():
            Deca_dict[key] = '{}_{}'.format(next_num, route)
            decaAdd_occurrences[value] = next_num, cnt-1
            changed_deca_dict[key] = '{}_{}_{}'.format(num, next_num, route)
        else:
            alpha_deca_dict[key] = value

This gives dictionaries:

Deca_dict -> {
    "1": "3_506",
    "2": "2_506",
    "3": "4_506",
    "4": "3_600",
    "5": "1_650",
    "6": "2_600"
}

changed_deca_dict = {'1':'2_3_506', '3':'2_4_506','4':'2_3_600'}

I have another dictionary with the same keys and their distances along an axis (the order in which I'm trying to number these values)

distance_dict = {'3': 1.0,'1': 2.2,'2': 3.1,'4': 2.0,'5': 1.5,'6': 3.0}

How can I either incorporate into the previous code or update Deca_dict like this:

Deca_dict -> {
    "1": "3_506",
    "2": "4_506",
    "3": "2_506",
    "4": "2_600",
    "5": "1_650",
    "6": "3_600"
}

I'm trying to start by grouping the keys by similar values. I don't want the whole value compared, so this is what I have so far:

for deca_key, deca_value in changed_deca_dict:
    old_num = value.split('_')[0]
    new_num = value.split('_')[1]
    route = value.split('_')[2]
    old_add = str(old_num) + str(route)
    for dist_key, dist_value in distance_dict:
        if dist_key == deca_key:
            check the distance and compare with other Deca_dict.items(), including the one that didn't change
            re-order new_num according to distance along route
Community
  • 1
  • 1
user25976
  • 1,005
  • 4
  • 18
  • 39
  • Wouldn't it be more elegant, instead of encoding the duplicate values in a string, to collect the duplicates in a set (if no repetitions are allowed) or a list - and have these as attributes? Something like `changed_deca_dict = {'1': {v1:set([2, 3]), v2:506}, {'3':{v1:set([2, 4]), v2:506}, {'4': {v1:set([2, 3]), v2:600}}` – boardrider Jun 06 '15 at 01:39
  • Hm....okay, how can I get there? why is that better? – user25976 Jun 06 '15 at 11:36
  • _I_ think it's more elegant: you may think differently... – boardrider Jun 06 '15 at 15:43
  • @boardrider I may--but I'm not even sure how to get to this dictionary. Also, where would you go from there? – user25976 Jun 09 '15 at 00:20

0 Answers0