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

getting highest smallest floats values from mixed list with inf in list

list float values & inf repeat many times inside list

the mission is extract the highest float value & smallest float value
Noting that : there are inf appears in the list

**my code :**

list  = (inf , inf , inf , inf , 0.9 , 0.5 , 2.5 , 3.5 , 4.9 , 9.9 , 0.2)


def get_highest_lowest():
    lowest = 0
    highest = 0


    for i in list:
        if i >= 0 and i != float('inf'):

            # get lowest value
            if lowest > i:
                lowest = i
                

            # get highest value
            if highest < i:
                highest = i
                

        

    return [highest , lowest]

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

>Solution :

You may want to set initial lowest as np.inf

import numpy as np
lst  = (np.inf , np.inf , np.inf , np.inf , 0.9 , 0.5 , 2.5 , 3.5 , 4.9 , 9.9 , 0.2)


def get_highest_lowest():
    lowest = np.inf
    highest = -np.inf


    for i in lst:
        if i >= 0 and i != float('inf'):

            # get lowest value
            if lowest > i:
                lowest = i
                

            # get highest value
            if highest < i:
                highest = i
                

        

    return [highest , lowest]

get_highest_lowest()
>>> [9.9, 0.2]
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