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

Question:

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]

Asked By: NasserCzar

||

Answers:

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]
Answered By: Ricardo
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.