Python 我如何找到2个数字的最大值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3357369/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 10:38:18  来源:igfitidea点击:

How do I find the maximum of 2 numbers?

pythonmax

提问by Shilpa

How to find the maximum of 2 numbers?

如何找到2个数字的最大值?

value = -9999
run = problem.getscore()

I need to compare the 2 values i.e valueand runand find the maximum of 2. I need some python function to operate it?

我需要比较两个值 ie valueandrun并找到 2 的最大值。我需要一些 python 函数来操作它吗?

采纳答案by Ashley Grenon

Use the builtin function max.

使用内置函数max

Example: max(2, 4)returns 4.

示例: max(2, 4)返回 4。

Just for giggles, there's a minas well...should you need it. :P

只是为了咯咯的笑声,还有一个min……如果你需要它。:P

回答by dave

max(number_one, number_two)

max(number_one, number_two)

回答by Ignacio Vazquez-Abrams

回答by Chris B.

You can use max(value, run)

您可以使用 max(value, run)

The function maxtakes any number of arguments, or (alternatively) an iterable, and returns the maximum value.

该函数max接受任意数量的参数,或(或者)一个可迭代对象,并返回最大值。

回答by Tim Pietzcker

max(value,run)

should do it.

应该这样做。

回答by Muhammad Alkarouri

Just for the fun of it, after the party has finished and the horse bolted.

只是为了好玩,在派对结束并且马狂奔之后。

The answer is: max()!

答案是:max()

回答by Ivranovi

I noticed that if you have divisions it rounds off to integer, it would be better to use:

我注意到如果你有除法,它会四舍五入到整数,最好使用:

c=float(max(a1,...,an))/b

c=float(max(a1,...,an))/b

Sorry for the late post!

对不起,迟到的帖子!

回答by Ryan

numberList=[16,19,42,43,74,66]

largest = numberList[0]

for num2 in numberList:

    if num2 > largest:

        largest=num2

print(largest)

gives largest number out of the numberslist without using a Max statement

在不使用 Max 语句的情况下给出数字列表中的最大数字

回答by Mason

(num1>=num2)*num1+(num2>num1)*num2will return the maximum of two values.

(num1>=num2)*num1+(num2>num1)*num2将返回两个值中的最大值。

回答by Dimitris Fasarakis Hilliard

You could also achieve the same result by using a Conditional Expression:

您还可以使用条件表达式获得相同的结果:

maxnum = run if run > value else value

a bit more flexible than maxbut admittedly longer to type.

max打字更灵活,但不可否认的是打字时间更长。