[python] Python: finding lowest integer

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?

This question is related to python list integer

The answer is


number_list = [99.5,1.2,-0.3]

number_list.sort()

print number_list[0]

Or no float conversion at all by just specifying floats in the list.

l = [-1.2, 0.0, 1]
x = min(l)

or

l = min([-1.2, 0.0, 1])

list1 = [10,-4,5,2,33,4,7,8,2,3,5,8,99,-34]

print(list1)

max_v=list1[0]

min_v=list1[0]

for x in list1:
    if x>max_v:
        max_v=x
        print('X is {0} and max is {1}'.format(x,max_v))
for x in list1:
    if x<min_v:
        min_v=x
        print('X is {0} and min is {1}'.format(x,min_v))

print('Max values is ' + str(max_v))
print('Min values is ' + str(min_v))

You have to start somewhere the correct code should be:

The code to return the minimum value

l = [ '0.0', '1','-1.2'] x = l[0] for i in l: if i < x: x = i print x

But again it's good to use directly integers instead of using quotations ''

This way!

l = [ 0.0, 1,-1.2] x = l[0] for i in l: if i < x: x = i print x


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

Or, if you want the result as a string, rather than a float, use a key function:

x = min(l, key=float)

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)

l = [-1.2, 0.0, 1]

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

This is the answer, i needed this for my homework, took your code, and i deleted the " " around the numbers, it then worked, i hope this helped


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.)


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


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.


'''Functions'''

import math

#functions
def min3(x1,x2,x3):
    if x1<= x2 and x1<= x3:
        return x1
    elif x2<= x1 and x2<= x3:
        return x2
    elif x3<= x2 and x3<= x1:
          return x3
print(min3(4, 7, 5))

print(min3(4, 5, 5))

print(min3(4, 4, 4))

print(min3(-2, -6, -100))

print(min3("Z", "B", "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

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'

Examples related to python

programming a servo thru a barometer Is there a way to view two blocks of code from the same file simultaneously in Sublime Text? python variable NameError Why my regexp for hyphenated words doesn't work? Comparing a variable with a string python not working when redirecting from bash script is it possible to add colors to python output? Get Public URL for File - Google Cloud Storage - App Engine (Python) Real time face detection OpenCV, Python xlrd.biffh.XLRDError: Excel xlsx file; not supported Could not load dynamic library 'cudart64_101.dll' on tensorflow CPU-only installation

Examples related to list

Convert List to Pandas Dataframe Column Python find elements in one list that are not in the other Sorting a list with stream.sorted() in Java Python Loop: List Index Out of Range How to combine two lists in R How do I multiply each element in a list by a number? Save a list to a .txt file The most efficient way to remove first N elements in a list? TypeError: list indices must be integers or slices, not str Parse JSON String into List<string>

Examples related to integer

Python: create dictionary using dict() with integer keys? How to convert datetime to integer in python Can someone explain how to append an element to an array in C programming? How to get the Power of some Integer in Swift language? python "TypeError: 'numpy.float64' object cannot be interpreted as an integer" What's the difference between integer class and numeric class in R PostgreSQL: ERROR: operator does not exist: integer = character varying C++ - how to find the length of an integer Converting binary to decimal integer output Convert floats to ints in Pandas?