四舍五入Python的数字列表并保持其总和 [英] Round a Python list of numbers and maintain their sum

查看:160
本文介绍了四舍五入Python的数字列表并保持其总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Python中有一个列表或十进制数字数组.我需要将它们四舍五入到最接近的小数点后两位.但是,我需要保持总和,即原始数组的舍入到小数点后2位的总和必须等于数组舍入后的元素的总和.

I have a list or an array of decimal numbers in Python. I need to round them to the nearest 2 decimal places as these are monetary amounts. But, I need the overall sum to be maintained, i.e. the sum of the original array rounded to 2 decimal places must be equal to the sum of the rounded elements of the array.

到目前为止,这是我的代码:

Here's my code so far:

myOriginalList = [27226.94982, 193.0595233, 1764.3094, 12625.8607, 26714.67907, 18970.35388, 12725.41407, 23589.93271, 27948.40386, 23767.83261, 12449.81318]
originalTotal = round(sum(myOriginalList), 2)
# Answer = 187976.61

# Using numpy
myRoundedList = numpy.array(myOriginalList).round(2)
# New Array = [ 27226.95    193.06   1764.31  12625.86  26714.68  18970.35  12725.41 23589.93 27948.4   23767.83  12449.81]

newTotal = myRoundedList.sum()
# Answer = 187976.59

我需要一种有效的方法来修改新的舍入数组,使总和也为187976.61.需要将2便士的差异应用于项目7和6,因为这四舍五入后的条目与原始条目之间的差异最大.

I need an efficient way of amending my new rounded array such that the sum is also 187976.61. The 2 pence difference needs to be applied to items 7 and 6 as these have the greatest difference between the rounded entries and the original entries.

推荐答案

第一步是计算所需结果与实际总和之间的误差:

The first step is to calculate the error between the desired result and the actual sum:

>>> error = originalTotal - sum(myRoundedList)
>>> error
0.01999999996041879

这可以是正数或负数.由于myRoundedList中的每个项目都在实际值的0.005以内,因此该误差将小于原始数组的每个项目0.01.您可以简单地除以0.01并四舍五入以获得必须调整的项目数:

This can be either positive or negative. Since every item in myRoundedList is within 0.005 of the actual value, this error will be less than 0.01 per item of the original array. You can simply divide by 0.01 and round to get the number of items that must be adjusted:

>>> n = int(round(error / 0.01))
>>> n
2

现在剩下的就是选择应调整的项目.最佳结果来自于首先调整最接近边界的那些值.您可以通过按原始值和四舍五入后的值之间的差异进行排序来找到这些值.

Now all that's left is to select the items that should be adjusted. The optimal results come from adjusting those values that were closest to the boundary in the first place. You can find those by sorting by the difference between the original value and the rounded value.

>>> myNewList = myRoundedList[:]
>>> for _,i in sorted(((myOriginalList[i] - myRoundedList[i], i) for i in range(len(myOriginalList))), reverse=n>0)[:abs(n)]:
    myNewList[i] += math.copysign(0.01, n)

>>> myRoundedList
[27226.95, 193.06, 1764.31, 12625.86, 26714.68, 18970.35, 12725.41, 23589.93, 27948.4, 23767.83, 12449.81]
>>> myNewList
[27226.95, 193.06, 1764.31, 12625.86, 26714.68, 18970.359999999997, 12725.42, 23589.93, 27948.4, 23767.83, 12449.81]
>>> sum(myNewList)
187976.61

这篇关于四舍五入Python的数字列表并保持其总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆