Home > Back-end >  How can I use min() without getting "0" or "" for an answer?
How can I use min() without getting "0" or "" for an answer?

Time:11-24

I'm trying to use min in a list that came from a .csv and some of the values are '' how can I ignore those and also "0"?

I tried

index1 = (life_expectancy.index(min(life_expectancy,)))
print(life_expectancy[index1])

and got nothing, when i tried:

index1 = (life_expectancy.index(min(life_expectancy, key=int)))

I got: ValueError: invalid literal for int() with base 10: ''

because this is the value that the function treats at the min

CodePudding user response:

I don't think there is a simple way to do this in a single line of code using min.

One way to avoid 0-values is to call filter before min. Then, to avoid invalid values, you can write a wrapper around int that returns 0 on invalid values.

def int_or_zero(s):
    try:
        return int(s)
    except ValueError:
        return 0

def nonzero_min(seq):
    return min(filter(None, map(int_or_zero, seq)))

print( nonzero_min(['hello', '0', '12', '3', '0', '5', '']) )
# 3

Another way is to write the whole function "manually" with a for-loop.

def nonzero_min2(seq):
    m = 9999999
    for x in seq:
        try:
            x = int(x)
            if x < m and x != 0:
                m = x
        except ValueError:
            pass
    return m

print( nonzero_min2(['hello', '0', '12', '3', '0', '5', '']) )
# 3

CodePudding user response:

Try this:

new_life_expectancy = [value for value in life_expectancy if value != '' and value != '0']

This removes all instances of '' and 0

CodePudding user response:

The problem statement is incomplete, so we cannot conclude.

Is it a list of strings ? integers ? floats ? with None ? with float("NaN") ? a mix of all ?


With a list of mixed numbers and strings, you would have:

life_expectancy= [2,3,5,7,11,13, "", 0, "", 3.14, 1.414, "", 1.712, "", 1.618, 0]
index1 = (life_expectancy.index(min(life_expectancy,)))
TypeError: '<' not supported between instances of 'str' and 'int'

index1 = (life_expectancy.index(min(life_expectancy,key=int)))
ValueError: invalid literal for int() with base 10: ''

With a list of mixed numbers, None, and strings, you would have:

life_expectancy= [2,3,5,7,11,13, None, 0, None, 3.14, 1.414, None, 1.712, None, 1.618, 0]
index1 = (life_expectancy.index(min(life_expectancy,)))
TypeError: '<' not supported between instances of 'NoneType' and 'int'

index1 = (life_expectancy.index(min(life_expectancy,key=int)))
TypeError: int() argument must be a string, a bytes-like object or a real number, not 'NoneType'

So, yeah ... it all depends on your actual inputs... Stef's answer should do the trick.

otherwise:

life_expectancy= [2,3,5,7,11,13, "", "0", "", "3.14", 1.414, "", 1.712, "", 1.618, 0]
new_list = [e for e in life_expectancy if e and type(e) != str]
new_list
>>> [2, 3, 5, 7, 11, 13, 1.414, 1.712, 1.618]
  • revise your CSV loading.

you may consider using pandas and its read_csv() function, quopting:

Read a comma-separated values (csv) file into DataFrame.

Also supports optionally iterating or breaking of the file into chunks.

Give a special look to all the doc/parameters, because there are a lot of default values and inferences, especially:

sepstr, default ‘,’

Delimiter to use.

and:

decimalstr, default ‘.’

Character to recognize as decimal point (e.g. use ‘,’ for European data).

(and a lot more)

CodePudding user response:

you can try something like :

df_temp = df.loc[df["column_name"]!=""]

df_temp = df_temp.loc[df_temp["column_name"]>0]

Im not sure if you could just do != "" | 0, not sure if loc supports double condition.

Then you can just do a min on that df_temp

CodePudding user response:

#You can simply remove all 0 terms and string terms from your list and then use min
l=[1,34,6,4,1,4,0]
l1=l
while True:
    try:
        l1.remove(0)
    except ValueError:
        break    
print(min(l1))   
  • Related