máximo de 2 números


¿Cómo encontrar el máximo de 2 números?

value = -9999
run = problem.getscore()

Necesito comparar los 2 valores es decir value y run y encontrar el máximo de 2. ¿Necesito alguna función de Python para operarla?

 73
Author: lospejos, 2010-07-29

10 answers

Utilice la función builtinmax.

Ejemplo: max(2, 4) devuelve 4.

Solo para risitas, hay un min también...si lo necesitas. : P

 167
Author: Ashley Grenon,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-09-19 15:23:52
 21
Author: Ignacio Vazquez-Abrams,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-07-28 20:50:14

max(number_one, number_two)

 12
Author: dave,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-07-28 20:56:44

Puedes usar max(value, run)

La función max toma cualquier número de argumentos, o (alternativamente) un iterable, y devuelve el valor máximo.

 8
Author: Chris B.,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-07-28 20:50:33
max(value,run)

Debería hacerlo.

 7
Author: Tim Pietzcker,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-07-28 20:50:34

Solo por diversión, después de que la fiesta haya terminado y el caballo haya huido.

La respuesta es: max() !

 4
Author: Muhammad Alkarouri,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-07-28 22:02:33

También podría lograr el mismo resultado usando una Expresión condicional :

maxnum = run if run > value else value

Un poco más flexible que max pero es cierto que más tiempo para escribir.

 3
Author: Jim Fasarakis Hilliard,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-11-23 19:08:48

Me di cuenta de que si tienes divisiones que redondea a entero, sería mejor utilizar:

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

Lo siento por el post tardío!

 1
Author: Ivranovi,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-04-25 15:33:01
numberList=[16,19,42,43,74,66]

largest = numberList[0]

for num2 in numberList:

    if num2 > largest:

        largest=num2

print(largest)

Da el número más grande de la lista numberslist sin usar una sentencia Max

 1
Author: Ryan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-06-09 02:13:27

(num1>=num2)*num1+(num2>num1)*num2 devolverá el máximo de dos valores.

 1
Author: Mason,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-09-22 22:52:49