user9673470
user9673470

Reputation: 79

I want to replace dictionary's value

I want to replace dictionary's value.I have a dictionary whose variable's name is dct like

dct={'A': {'a1': [[10.0, 5.0], [7.0, 7.0], [1.0, 5.0], [20.0, 30.0]], 
           'a2': [[50.0, 50.0], [55.0, 60.0]], 
           'a3': [[40.0, 100.0], [100.0, 200.0], [100.0, 140.0], [200.0, 190.0]], 
           'a4': [[50.0, 70.0], [140.0, 130.0], [160.0, 150.0], [200.0, 180.0]], 
           'a5': [[100.0, 110.0], [180.0, 210.0], [60.0, 50.0], [200.0, 190.0]] }}

If dictionary's child value like [[10.0, 5.0], [7.0, 7.0], [1.0, 5.0], [20.0, 30.0]] or [[50.0, 50.0], [55.0, 60.0]] can be divided 4,I want to replace 5 instead of the child value.If dictionary's child value can be divided 2,I want to replace 4 instead of the child value. So, I wrote the codes,

  for ky, vl in dct.items():
        for k,v in vl.items():
            if len(v) %4 == 0:
                    element[ky] = 5
            elif len(v) %2 == 0:
                    element[ky] = 4
            else:
                    continue
  print(element)

But print(element) shows {‘A’: {‘a5’: 5}} so it has only last value.I really cannot understand why such a thing happens.How can I fix this?What is wrong in my codes?

Upvotes: 1

Views: 91

Answers (2)

Aditya Mandal
Aditya Mandal

Reputation: 188

Actually your code is incorrect to perform that given task, here's the correct code to solve your query like whatever you wanted to implement. Check this below code it works fine and replaces child values by 5 when each child value is divisible by 4 and replaces values by 4 when each child value is divisible by 2

dct = {'A': {'a1': [[10.0, 5.0], [7.0, 7.0], [1.0, 5.0], [20.0, 30.0]],
           'a2': [[50.0, 50.0], [55.0, 60.0]],
           'a3': [[40.0, 100.0], [100.0, 200.0], [100.0, 140.0], [200.0, 190.0]],
           'a4': [[50.0, 70.0], [140.0, 130.0], [160.0, 150.0], [200.0, 180.0]],
           'a5': [[100.0, 110.0], [180.0, 210.0], [60.0, 50.0], [200.0, 190.0]] }}

print (dct)
for k,v in dct.items():
    for ky,vl in v.items():
        for each_elem in (range(0,len(vl))):
            if vl[each_elem][0] % 4 == 0:
                vl[each_elem][0] = 5
            else:
                if vl[each_elem][0] % 2 == 0:
                    vl[each_elem][0] = 4

            if vl[each_elem][1] % 4 == 0:
                vl[each_elem][1] = 5
            else:
                if vl[each_elem][1] % 2 == 0:
                    vl[each_elem][1] = 4

print ("\n")   
print (dct)

that gives this output below

{'A': {'a1': [[10.0, 5.0], [7.0, 7.0], [1.0, 5.0], [20.0, 30.0]], 'a3': [[40.0, 100.0], [100.0, 200.0], [100.0, 140.0], [200.0, 190.0]], 'a2': [[50.0, 50.0], [55.0, 60.0]], 'a5': [[100.0, 110.0], [180.0, 210.0], [60.0, 50.0], [200.0, 190.0]], 'a4': [[50.0, 70.0], [140.0, 130.0], [160.0, 150.0], [200.0, 180.0]]}}


{'A': {'a1': [[4, 5.0], [7.0, 7.0], [1.0, 5.0], [5, 4]], 'a3': [[5, 5], [5, 5], [5, 5], [5, 4]], 'a2': [[4, 4], [55.0, 5]], 'a5': [[5, 4], [5, 4], [5, 4], [5, 4]], 'a4': [[4, 4], [5, 4], [5, 4], [5, 5]]}}

Hope this answer work great for you. Have a good time ahead :)

Upvotes: 1

Aaditya Ura
Aaditya Ura

Reputation: 12669

The problem is you are inserting main dict key's into new dict , But in origional dict there are two dict , so you have to maintain a sub or nested dict and then at last you can insert that nested dict to main dict:

Try this code :

dct={'A': {'a1': [[10.0, 5.0], [7.0, 7.0], [1.0, 5.0], [20.0, 30.0]], 'a2': [[50.0, 50.0], [55.0, 60.0]], 'a3': [[40.0, 100.0], [100.0, 200.0], [100.0, 140.0], [200.0, 190.0]], 'a4': [[50.0, 70.0], [140.0, 130.0], [160.0, 150.0], [200.0, 180.0]], 'a5': [[100.0, 110.0], [180.0, 210.0], [60.0, 50.0], [200.0, 190.0]] }}
element={}
for ky, vl in dct.items():
    sub_dict={}
    for k, v in vl.items():
        if len(v) % 4 == 0:
            sub_dict[k] = 5
        elif len(v) % 2 == 0:
            sub_dict[k] = 4
        else:
            continue
    element[ky]=sub_dict
print(element)

output:

{'A': {'a1': 5, 'a2': 4, 'a3': 5, 'a5': 5, 'a4': 5}}

Upvotes: 0

Related Questions