Python: finding lowest integer

前端 未结 13 704
北海茫月
北海茫月 2020-12-05 18:16

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 fin

相关标签:
13条回答
  • 2020-12-05 18:31

    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
    
    0 讨论(0)
  • 2020-12-05 18:36

    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)
    
    0 讨论(0)
  • 2020-12-05 18:37
    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

    0 讨论(0)
  • 2020-12-05 18:41

    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)
    
    0 讨论(0)
  • 2020-12-05 18:43

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

    0 讨论(0)
  • 2020-12-05 18:43

    '''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"))
    
    0 讨论(0)
提交回复
热议问题