Home > Enterprise >  getting highest smallest floats values from mixed list with inf in list
getting highest smallest floats values from mixed list with inf in list

Time:12-10

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]

CodePudding user response:

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]
  • Related