Python:找到最小整数

发布于 2024-08-28 09:02:28 字数 193 浏览 8 评论 0 原文

我有以下代码:

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

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

该代码应该找到列表中的最低值(-1.2),但是当我打印“x”时,它发现该值仍然是 100.0 我的代码哪里出错了?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

故事还在继续 2024-09-04 09:02:28

要查找列表的最小值,您也可以使用 min

x = min(float(s) for s in l) # min of a generator

或者,如果您希望结果为字符串而不是浮点数,请使用 关键功能

x = min(l, key=float)

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)
じее 2024-09-04 09:02:28

您不是在比较整数,而是在比较字符串。字符串按字典顺序进行比较(即逐个字符进行比较),而不是(如您似乎想要的那样)通过将值转换为浮点数进行比较。让您的列表保存数字(浮点数或整数,具体取决于您想要的),或者在比较它们之前将字符串转换为循环中的浮点数或整数。

您可能还对 min 内置函数感兴趣,它已经完成了当前循环所做的事情(也就是说,没有转换。)

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

顾北清歌寒 2024-09-04 09:02:28

看起来您想要将列表转换为数字列表

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

,或者如果它确实是您想要的字符串,那么您想要使用 minkey 参数

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

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'
屋檐 2024-09-04 09:02:28

Python 有一个内置的 min 函数来帮助您找到最小的。

但是,您需要将列表项转换为数字,然后才能找到最小的整数(什么,这不是浮点数吗?)

min(float(i) for i in l)

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)
世俗缘 2024-09-04 09:02:28

l 是字符串列表。当您像这样将数字放在单引号之间时,您正在创建字符串,它们只是一个字符序列。为了让你的代码正常工作,你必须这样做:

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

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

如果你必须使用字符串列表,你可以尝试让Python尝试从每个字符串中生成一个数字。这类似于 Justin 的回答,只是它理解浮点(十进制)数字正确。

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

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

我希望您编写的代码是为了学习 Python 或一般编程。如果是这样的话,那就太好了。但是,如果这是生产代码,请考虑使用 Python 的内置函数。

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

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
德意的啸 2024-09-04 09:02:28
number_list = [99.5,1.2,-0.3]

number_list.sort()

print number_list[0]
number_list = [99.5,1.2,-0.3]

number_list.sort()

print number_list[0]
绮筵 2024-09-04 09:02:28

在进行比较之前将变量转换为浮点数:

if float(i) < float(x):

问题是您正在将字符串与浮点数进行比较,这是行不通的。

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.

狼性发作 2024-09-04 09:02:28
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))
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))
葮薆情 2024-09-04 09:02:28

或者仅通过在列表中指定浮点数来​​根本不进行浮点数转换。

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

或者

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

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])
暗恋未遂 2024-09-04 09:02:28

您必须从正确的代码应该是的地方开始:

返回最小值的代码


l = ['0.0','1','-1.2']
x = l[0]
对于 l 中的 i:
如果我< x:
x = 我
打印 x

但再次强调,最好直接使用整数,而不是使用引号 ''

这样

<代码>
l = [ 0.0, 1,-1.2]
x = l[0]
对于 l 中的 i:
如果我< x:
x = 我
打印 x

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

欢你一世 2024-09-04 09:02:28
l = [-1.2, 0.0, 1]

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

这就是答案,我的作业需要这个,拿了你的代码,然后我删除了数字周围的“”,然后它就起作用了,我希望这有帮助

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

独行侠 2024-09-04 09:02:28

列表中有字符串,并且您正在将它们与数字 100.0 进行比较。

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

自我难过 2024-09-04 09:02:28

'''功能'''

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

'''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"))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文