简体   繁体   中英

Algorithm to keep a list of percentages to add up to 100%

(code examples are python)
Lets assume we have a list of percentages that add up to 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.
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.

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

The goal is to add a new item to mylist, while sum(mylist) stays 100.0 and vari
items keep their relations to each other. For that we need to substract a CERTAIN
PERCENTAGE from each vari item. Example: lets assume we want to add 4.0 to mylist.
Using an ugly aproximation loop I found out that i need to substract ca. 5.0634%
of each vari item (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 . 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

sum(r(i)) == -n

or

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

Therefore, the weighting function is -(n*i)/sum(v)

you're being silly.

let's say you want to add 4.0 to the list. 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

you want to add 20.0 as an item. so then you multiply each item by 0.8, which is (100-20)*0.01

update: Hrmn I didn't read carefuly enough.

think of it like this. (fixed)+(vari)= 100; (fixed)+(vari * x) + newitem = 100;

so basically like what we did before except with just the vari portion. if vari totals to 50, and the new item you're adding is 3.0, then multiply each item in vari by (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:

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

I think that your constant 5.0634 should actually be 5.0633.

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