Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Redistribution of int list with upper limit

I have few numbers like a parts of score:

  • 10 points
  • 5 points
  • 20 points
  • 30 points
  • 25 points
  • 10 points

my_list = [10, 5, 20, 30, 25, 10]

sum of my points is 100 – it is maximum

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I want to normalize it knowing that the sum should not exceed 100. How can I do this?
so if i add another element with score 10, from the rest, a numbers will be subtracted so that the sum becomes 100…

like I’m moving the sliders in the game while having the maximum set of parameter points for my hero, IDK

Is there any library for this process? in numpy|sklearn maybe?

>Solution :

You can write a custom function to do this — no heavy dependencies required. Note that this returns a new list; one could further improve this by creating a class:

def generate_list_with_normalization(input_list, element_to_add, cap=100):
    total = sum(input_list) + element_to_add
    
    if total <= cap:
        return input_list + [element_to_add]
    return [elem * cap / total for elem in input_list + [element_to_add]]

my_list = [10, 5, 20, 30, 25, 10]
result = generate_list_with_normalization(my_list, 10, 100)
print(result)
print(sum(result))

This outputs:

[9.090909090909092, 4.545454545454546, 18.181818181818183,
27.272727272727273, 22.727272727272727, 9.090909090909092, 9.090909090909092]
100.0
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading