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
How do I find the maximum of 2 numbers?
提问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
回答by dave
max(number_one, number_two)
max(number_one, number_two)
回答by Chris B.
回答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打字更灵活,但不可否认的是打字时间更长。

