results = []
forecast = [10.5, 20.6, 30.8, 5.4, 0, 1, 1.4, 2.2]
actual_result = 65
rounded_list = [round(number) for number in forecast if number >= 0]
percentage = actual_result / sum(rounded_list)
for number in rounded_list:
results.append(number * percentage)
Print list:
[9.154929577464788, 19.225352112676056, 28.380281690140844, 4.577464788732394, 0.0, 0.9154929577464789, 0.9154929577464789, 1.8309859154929577]
Print sum list:
65.0
If you want to be very precise and the forecast list should only be integer:
results = []
forecast = [10.5, 20.6, 30.8, 5.4, 0, 1, 1.4, 2.2]
actual_result = 65
rounded_list = [number * 10 for number in forecast if number >= 0]
percentage = 65 * 10 / sum(rounded_list)
for number in rounded_list:
results.append(number * percentage / 10)
Print list:
[9.492350486787204, 18.6230876216968, 27.8442280945758, 4.881780250347704, 0.0, 0.9040333796940194, 1.265646731571627, 1.9888734353268425]
Print list sum:
65.0
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…