[Python] Ordering Inputs in a Function then Performing Calculations in One Line

Discussão em 'Python' iniciado por Stack, Fevereiro 6, 2025.

  1. Stack

    Stack Membro Participativo

    I am looking to create a function where I can input four numbers, order them to find the two smallest, and then perform calculations on the two smallest in a single line if possible (I don't want to do a bunch of if-else statements) and I feel like there should be a faster way even without a lambda function.

    Ex of what I would like it to work like: def square(a, b, c, d): return [x*2 for x in min(a, b, c, d)]

    I tried a couple attempts to use sort() on a list, or use pop to get rid of the max numbers, but I can't figure out how to do this in a single line.

    Continue reading...

Compartilhe esta Página