[Python] sorted with random as key [duplicate]

Discussão em 'Python' iniciado por Stack, Maio 13, 2025.

  1. Stack

    Stack Membro Participativo

    how sorted_numbers = sorted(numbers, key=lambda x: random.random()) work ? for example


    numbers = [34, 12, 5, 67, 23]
    sorted_numbers = sorted(numbers, key=lambda x: random.random())

    print(sorted_numbers)```



    In [5]: numbers = [34, 12, 5, 67, 23]

    In [6]: sorted_numbers = sorted(numbers, key=lambda x: random.random())

    In [7]: sorted_numbers
    Out[7]: [23, 67, 12, 34, 5]

    In [9]: sorted_numbers = sorted(numbers, key=lambda x: random.random())

    In [10]: sorted_numbers
    Out[10]: [12, 5, 67, 23, 34]

    Continue reading...

Compartilhe esta Página