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

Python to bin calculated results from a function

A function is defined to perform some calculation (in this example, it’s to sum). The calculated result is to be put into bins (of each 10 as an interval, such as <10, 10-19, 20-29 etc).

What is the smart way to do so? Thank you.

I have a dump way, which created a dictionary to list all the possible calculated results and their bins:

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

def total(iterable):
    dict = {36: '30 - 40' , 6 : '< 10'}
    total = dict[sum(iterable)]
    return total



candidates = [[11,12,13],[1,2,3]]

for iterable in candidates:
    output = str(total(iterable))
    print (output)

>Solution :

Using a dict will not be feasible, since you possibly cannot have all the options over there, here is how you can do it

candidates = [[11,12,13], [1,2,3]]

for iterable in candidates:
    sum1 = sum(iterable)
    start_bin = int(sum1/10) * 10
    end_bin = start_bin + 10
    print('{} - {}'.format(start_bin, end_bin))

You can also make variations in the size of bin by changing how the values are multiplied and divided

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