views:

202

answers:

7

I have the following code:

l = ['-1.2', '0.0', '1']

x = 100.0
for i in l:
    if i < x:
        x = i
print x

The code should find the lowest value in my list (-1.2) but instead when i print 'x' it finds the value is still 100.0 Where is my code going wrong?

+8  A: 

You aren't comparing integers, you're comparing strings. Strings compare lexicographically -- meaning character by character -- instead of (as you seem to want) by converting the value to a float. Make your list hold numbers (floats or integers, depending on what you want), or convert the strings to floats or integers in your loop, before you compare them.

You may also be interested in the min builtin function, which already does what your current loop does (without the converting, that is.)

Thomas Wouters
+2  A: 

Cast the variable to a float before doing the comparison:

if float(i) < float(x):

The problem is that you are comparing strings to floats, which will not work.

Justin Ethier
Note that int('1.2') and int('0.0') will both be an error.
Thomas Wouters
why not float ?
LB
Thanks, serves me right for trying to answer a question early on a Monday :)
Justin Ethier
A: 

You have strings in the list and you are comparing them with the number 100.0.

Plamen Dragozov
+1  A: 

Python has a built in min function to help you with finding the smallest.

However, you need to convert your list items to numbers before you can find the lowest integer( what, isn't that float? )

min(float(i) for i in l)
Satoru.Logic
+1  A: 

l is a list of strings. When you put numbers between single quotes like that, you are creating strings, which are just a sequence of characters. To make your code work properly, you would have to do this:

l = [-1.2, 0.0, 1]  # no quotation marks

x = 100.0
for i in l:
    if i < x:
        x = i
print x

If you must use a list of strings, you can try to let Python try to make a number out of each string. This is similar to Justin's answer, except it understands floating-point (decimal) numbers correctly.

l = ['-1.2', '0.0', '1']

x = 100.0
for i in l:
    inum = float(i)
    if inum < x:
        x = inum
print x

I hope that this is code that you are writing to learn either Python or programming in general. If this is the case, great. However, if this is production code, consider using Python's built-in functions.

l = ['-1.2', '0.0', '1']
lnums = map(float, l)  # turn strings to numbers
x = min(lnums)  # find minimum value
print x
Wesley
+4  A: 

To find the minimum value of a list, you might just as well use min:

x = min(float(s) for s in l) # min of a generator
Olivier
`min` has a `key` argument for cases similar to this, with usage like `x = min(l, key=float)`. It is slightly different than the code posted in that it returns an item from the original iterable (i.e., the string `'-1.2'`) rather than the resultant thing (i.e. the float `-1.2`).
Mike Graham
Good point, Mike.
Olivier
+2  A: 

It looks like you want to convert the list to a list of numbers

>>> foo = ['-1.2', '0.0', '1']
>>> bar = map(float, foo)
>>> bar
[-1.2, 0.0, 1.0]
>>> min(bar)
-1.2

or if it really is strings you want, that you want to use min's key argument

>>> foo = ['-1.2', '0.0', '1']
>>> min(foo, key=float)
'-1.2'
Mike Graham