简体   繁体   中英

Percentage difference between values in dictionary

I have a dictionary as below

dictSumEachDay = {'2019.09.17': (558, 558),
                  '2019.09.09': (541, 552),
                  '2019.09.07': (415, 415),
                  '2019.09.11': (817, 817),
                  '2019.09.10': (755, 751),
                  '2019.09.12': (803, 771),
                  '2019.09.14': (679, 712),
                  '2019.09.06': (660, 654),
                  '2019.09.13': (737, 733),
                  '2019.09.15': (837, 837),
                  '2019.09.16': (715, 715),
                  '2019.09.08': (489, 492)}

And I'd like to compute the percentage difference between the values and return a dictionary such as

 dictPercentDiff = {'2019.09.17': 0.0,
                  '2019.09.09': 2.012808783165599,
                  '2019.09.07': 0.0,
                  '2019.09.11': 0.0,
                  '2019.09.10': 0.5312084993359893,
                  '2019.09.12': 4.066073697585769,
                  '2019.09.14': 4.744787922358016,
                  '2019.09.06': 0.91324200913242,
                  '2019.09.13': 0.5442176870748299,
                  '2019.09.15': 0.0,
                  '2019.09.16': 0.0,
                  '2019.09.08': 0.6116207951070336}

Already tried

dictPercentDiff = {}

for key, value in dictSumEachDay.items():
    top = abs(value[0] - value[1])
    bottom = (value[0] + value[1])/2
    dictPercentDiff[dictSumEachDay[key]] = (top/bottom)*100

Which returns:

{(558, 558): 0.0,
 (541, 552): 2.012808783165599,
 (415, 415): 0.0,
 (817, 817): 0.0,
 (755, 751): 0.5312084993359893,
 (803, 771): 4.066073697585769,
 (679, 712): 4.744787922358016,
 (660, 654): 0.91324200913242,
 (737, 733): 0.5442176870748299,
 (837, 837): 0.0,
 (715, 715): 0.0,
 (489, 492): 0.6116207951070336}

Obviously I'm mixing up keys and values but I can't seem to get it to work.

Assuming that you want the % difference keyed by the day, all you need to do is to correct your final dict assignment:

dictPercentDiff[key] = (top/bottom)*100

Your posted code very specifically uses the tuple of values as the new dict's key.

{key:(value[1]-value[0])/value[0] * 100 for key,value in dictSumEachDay.items()}

The problem lies with dictPercentDiff[dictSumEachDay[key]] . You're telling it that you want to take the key (date), find the value associated with that in dictSumEachDay (the tuple), and then use that as the key. If you just change it to dictPercentDiff[key] , that will get what you want. Also, using "numerator" and "denominator" rather than "top" and "bottom" is more clear. However, it's better to use a comprehension:

dictPercentDiff = {key: 200*abs(value[1]-value[0])/(value[0]+value[1]) for key, value in dictSumEachDay.items()}

As a further note, people generally use "percentage difference" to mean the signed difference as a percentage of the first value, while you are using it to mean the absolute value of the difference as a percentage of the average. That is, you are using 100*abs(value[1]-value[0])/((value[0]+value[1])/2) rather than 100*(value[1]-value[0])/(value[0]) .

below code is giving correct answer.

A ={'2019.09.17': (558, 558),
'2019.09.09': (541, 552),
'2019.09.07': (415, 415),
'2019.09.11': (817, 817),
'2019.09.10': (755, 751),
'2019.09.12': (803, 771),
'2019.09.14': (679, 712),
'2019.09.06': (660, 654),
'2019.09.13': (737, 733),
'2019.09.15': (837, 837),
'2019.09.16': (715, 715),
'2019.09.08': (489, 492)}

for i in A:
    if A[i][0]>A[i][-1]:
        print(abs(A[i][0]-A[i][-1])/A[i][-1]*100)
    else:
        print(abs(A[i][0]-A[i][-1])/A[i][0]*100)

Output :=>
0.0
2.033271719038817
0.0
0.0
0.5326231691078562
4.150453955901426
4.860088365243005
0.9174311926605505
0.5457025920873124
0.0
0.0
0.6134969325153374

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM