Home > Enterprise >  I am trying to replace the values in a dictionary with an updated one and i am not getting the exact
I am trying to replace the values in a dictionary with an updated one and i am not getting the exact

Time:11-30

So i updated the values of a dictionary into percentage by multiplying by 100. Now i want to replace the initial decimal with the updated results, but instead, i am getting each value replaced by the whole new values.

job_role_overtime_att_rate = {'Healthcare Representative Overtime Rate' : 2/37, ' Human Resources Overtime Rate': 5/13,
      'Laboratory Technician Total' : 31/62, 'Manager Total': 4/27, 'Manufacturing Director Total': 4/39,
      'Research Director Total' : 1/23, 'Research Scientist Total' : 33/97,
      'Sales Executive Total' : 31/94, 'Sales Representative Total' : 16/24}
job_role_overtime_att_rate()
{'Healthcare Representative Overtime Rate': 0.05405405405405406,
 ' Human Resources Overtime Rate': 0.38461538461538464,
 'Laboratory Technician Total': 0.5,
 'Manager Total': 0.14814814814814814,
 'Manufacturing Director Total': 0.10256410256410256,
 'Research Director Total': 0.043478260869565216,
 'Research Scientist Total': 0.3402061855670103,
 'Sales Executive Total': 0.32978723404255317,
 'Sales Representative Total': 0.6666666666666666}

this the result of the above code.

for i in job_role_overtime_att_rate.values():
    a = i * 100
    print('{0:.2f}'.format(a))

this multiplies the initial result by 100. now

5.41
38.46
50.00
14.81
10.26
4.35
34.02
32.98
66.67

here is the result.

for i in job_role_overtime_att_rate.values():
    a = i * 100
    print('{0:.2f}'.format(a))
    for values in job_role_overtime_att_rate.values():
        values = a
        print(values)

this is to replace the intial values with the new one. at least that's what i thought it will do.

5.41
5.405405405405405
5.405405405405405
5.405405405405405
5.405405405405405
5.405405405405405
5.405405405405405
5.405405405405405
5.405405405405405
5.405405405405405
38.46
38.46153846153847
38.46153846153847
38.46153846153847
38.46153846153847
38.46153846153847
38.46153846153847
38.46153846153847
38.46153846153847
38.46153846153847
50.00
50.0
50.0
50.0
50.0
50.0
50.0
50.0
50.0
50.0
14.81
14.814814814814813
14.814814814814813
14.814814814814813
14.814814814814813
14.814814814814813
14.814814814814813
14.814814814814813
14.814814814814813
14.814814814814813
10.26
10.256410256410255
10.256410256410255
10.256410256410255
10.256410256410255
10.256410256410255
10.256410256410255
10.256410256410255
10.256410256410255
10.256410256410255
4.35
4.3478260869565215
4.3478260869565215
4.3478260869565215
4.3478260869565215
4.3478260869565215
4.3478260869565215
4.3478260869565215
4.3478260869565215
4.3478260869565215
34.02
34.02061855670103
34.02061855670103
34.02061855670103
34.02061855670103
34.02061855670103
34.02061855670103
34.02061855670103
34.02061855670103
34.02061855670103
32.98
32.97872340425532
32.97872340425532
32.97872340425532
32.97872340425532
32.97872340425532
32.97872340425532
32.97872340425532
32.97872340425532
32.97872340425532
66.67
66.66666666666666
66.66666666666666
66.66666666666666
66.66666666666666
66.66666666666666
66.66666666666666
66.66666666666666
66.66666666666666
66.66666666666666

here is what it's returning. Kindly help. Thanks

CodePudding user response:

You can loop through the dict.items() function to loop through the key and the value.

job_role_overtime_att_rate = {
  'Healthcare Representative Overtime Rate': 0.05405405405405406,
  'Human Resources Overtime Rate': 0.38461538461538464,
  'Laboratory Technician Total': 0.5,
  'Manager Total': 0.14814814814814814,
  'Manufacturing Director Total': 0.10256410256410256,
  'Research Director Total': 0.043478260869565216,
  'Research Scientist Total': 0.3402061855670103,
  'Sales Executive Total': 0.32978723404255317,
  'Sales Representative Total': 0.6666666666666666
}

for name, value in job_role_overtime_att_rate.items():
  a = round(value*100, 2)
  job_role_overtime_att_rate[name] = a

print(job_role_overtime_att_rate)

CodePudding user response:

I'm not sure what your point is, but you loop over values and in the inner loop again over values. So for each value you loop over all values, thus it's expected that you will get n^2 prints for dictionary with n values.

If I guess right this code does what you need:

for key, value in job_role_overtime_att_rate.items():
    job_role_overtime_att_rate[key]= value*100
  • Related