简体   繁体   English

保持百分比列表的算法加起来达到100%

[英]Algorithm to keep a list of percentages to add up to 100%

(code examples are python) (代码示例是python)
Lets assume we have a list of percentages that add up to 100: 让我们假设我们有一个百分比列表,总计100:

mylist = [2.0, 7.0, 12.0, 35.0, 21.0, 23.0]  

Some values of mylist may be changed, others must stay fixed. 某些mylist值可能会更改,其他值必须保持固定。
Lets assume the first 3 (2.0, 7.0, 12.0) must stay fixed and the last three (35.0, 21.0, 23.0) may be changed. 让我们假设前3(2.0,7.0,12.0)必须保持固定,最后3(35.0,21.0,23.0)可能会改变。

fix = mylist[:3]
vari = mylist[3:]

The goal is to add a new item to mylist, while sum(mylist) stays 100.0 and vari 目标是将新项添加到mylist,而sum(mylist)保持100.0和vari
items keep their relations to each other. 物品保持彼此的关系。 For that we need to substract a CERTAIN 为此,我们需要减去一个CERTAIN
PERCENTAGE from each vari item. 每个变量项的PERCENTAGE。 Example: lets assume we want to add 4.0 to mylist. 示例:假设我们要将4.0添加到mylist。
Using an ugly aproximation loop I found out that i need to substract ca. 使用丑陋的aproximation循环我发现我需要减去ca. 5.0634% 5.0634%
of each vari item (CERTAIN PERCENTAGE = 5.0634): 每个变量项目(CERTAIN PERCENTAGE = 5.0634):

adjusted =[]
for number in vari:
    adjusted.append(number-(number*(5.0634/100.0)))
adjusted.extend(fix)
adjusted.append(4.0)

adjusted now contains my desired result. 现在调整包含我想要的结果。

My question is how to calculate CERTAIN PERCENTAGE ;.) 我的问题是如何计算某些百分比;。)

How's this? 这个怎么样?

def adjustAppend( v, n ):
    weight= -n/sum(v)
    return [ i+i*weight for i in v ] + [n]

Given a list of numbers v , append a new number, n . 给定一个数字列表v ,附加一个新数字, n Weight the existing number to keep the sum the same. 对现有数字进行加权以保持总和相同。

 sum(v) == sum( v + [n] )

Each element of v , i , must be reduced by some function of i , r ( i ) such that vi的每个元素必须通过iri )的某个函数来减少

sum(r(i)) == -n

or 要么

sum( map( r, v ) ) == -n

Therefore, the weighting function is -(n*i)/sum(v) 因此,加权函数是-(n*i)/sum(v)

you're being silly. 你很傻。

let's say you want to add 4.0 to the list. 假设您要将4.0添加到列表中。 You don't need to subtract a certain amount from each one. 您不需要从每个中减去一定数量。 What you need to do is multiply each item. 你需要做的是将每个项目相乘。

100 - 4 = 96. therefore, multiply each item by 0.96 100 - 4 = 96.因此,将每个项目乘以0.96

you want to add 20.0 as an item. 你想添加20.0作为项目。 so then you multiply each item by 0.8, which is (100-20)*0.01 那么你将每个项目乘以0.8,即(100-20)* 0.01

update: Hrmn I didn't read carefuly enough. 更新:Hrmn我没有仔细阅读。

think of it like this. 想到这就是这样。 (fixed)+(vari)= 100; (固定)+(vari)= 100; (fixed)+(vari * x) + newitem = 100; (固定)+(vari * x)+ newitem = 100;

so basically like what we did before except with just the vari portion. 所以基本上就像我们之前做的那样,只有vari部分。 if vari totals to 50, and the new item you're adding is 3.0, then multiply each item in vari by (47/50) 如果vari总计为50,并且您要添加的新项目为3.0,则将vari中的每个项目乘以(47/50)

new_item = 4.0
CERTAIN_PERCENTAGE = 100 * (float(new_item) / sum(vari)) 
NEW_NUMBER = 4.0

mylist = [2.0, 7.0, 12.0, 35.0, 21.0, 23.0]
fix = mylist[:3]
vari = mylist[3:]

weight = (100 - sum(fix) - NEW_NUMBER)/sum(vari)

adjusted = []
adjusted.extend( (weight*v for v in vari) )
adjusted.extend(fix)
adjusted.append(NEW_NUMBER)

print sum(adjusted)                # 100.0

Edit: Triptych is right, if you are actually interested in the certain percentage , the following code goes for it: 编辑:Triptych是对的,如果你真的对某个百分比感兴趣,下面的代码就是这样的:

certain_percentage = 100 * NEW_NUMBER / sum(vari)
print certain_percentage        # 5.06329113924

I think that your constant 5.0634 should actually be 5.0633. 我认为你的常量5.0634实际上应该是5.0633。

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

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