如何找到最多2个数字?

问题:如何找到最多2个数字?

如何找到最多2个数字?

value = -9999
run = problem.getscore()

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

How to find the maximum of 2 numbers?

value = -9999
run = problem.getscore()

I need to compare the 2 values i.e value and run and find the maximum of 2. I need some python function to operate it?


回答 0

使用内置功能 max

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

只是为了傻笑,还有一个min……您是否需要它。:P

Use the builtin function max.

Example: max(2, 4) returns 4.

Just for giggles, there’s a min as well…should you need it. :P


回答 1


回答 2

max(number_one, number_two)

max(number_one, number_two)


回答 3

您可以使用 max(value, run)

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

You can use max(value, run)

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


回答 4

max(value,run)

应该这样做。

max(value,run)

should do it.


回答 5

只是为了好玩,晚会结束后,马狂奔了。

答案是:max()

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

The answer is: max() !


回答 6

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

maxnum = run if run > value else value

max输入更灵活,但输入时间更长。

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

maxnum = run if run > value else value

a bit more flexible than max but admittedly longer to type.


回答 7

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

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


回答 8

我注意到,如果您有除法将其四舍五入为整数,则最好使用:

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

对不起,晚发!

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

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

Sorry for the late post!


回答 9

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

largest = numberList[0]

for num2 in numberList:

    if num2 > largest:

        largest=num2

print(largest)

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

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