简体   繁体   English

字典中值之间的百分比差异

[英]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: 假设您希望按日期输入%差异,您所需要做的就是更正最终dict分配:

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]] . 问题在于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. 您告诉它要获取键(日期),在dictSumEachDay (元组)中找到与该值关联的值,然后将其用作键。 If you just change it to dictPercentDiff[key] , that will get what you want. 如果只是将其更改为dictPercentDiff[key] ,则将获得所需的内容。 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]) . 也就是说,您使用的是100*abs(value[1]-value[0])/((value[0]+value[1])/2)而不是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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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