Hello, there is a function, part of which is presented below, which should calculate the difference between the elements of the list g and the lists, which are values ​​of the ang dictionary, and output the output in the form:

{'linear':[разница],'bent':[разница]} 

code itself:

  g = [132] ang = {'linear':[180],'bent':[120]} for k in ang: for i in g: for x in ang[k]: dict = {k: round(100*sum([abs(ix)])/sum(g)) for k,ang[k] in ang.items()} return(dict) 

however, only this is printed:

 {'linear':[36],'bent':[36]} 

although the answer would be considered correct:

 {'linear':[36],'bent':[9]} 

that is, it is considered only for linear, but not for bent

in all cases the number of elements g and the number of elements in the lists of the dictionary are the same

  • What is the correct answer? Explicitly give the expected answer and describe in words how this answer is from the presented values ​​of g , ang obtained. Do not use broken code as a task specification. - jfs
  • By what algorithm do you get 36, 9 from 132, 180, 120? - jfs
  • 36 = 100 * (132-180) / (132) 9 = 100 * (132-120) / (120) - pinacol

1 answer 1

You seem to be looking for a relative percentage difference:

 def rel_err_pct(a, b): return round(100 * abs(a - b) / b) 

Applying this function to the elements of lists that are values ​​in the dictionary:

 result = {key: list(map(rel_err_pct, g, lst)) for key, lst in ang.items()}