vendredi 27 novembre 2020

Python - How does argument 'function' affect a list when using shuffle

When you use shuffle(x, random), you have two parameters: the list and the function. By default, the function is random(), which returns a random value between 0 and 1.

Now, what I would like to know is, how does this affect the list? If, for example, I assign a function to return a specific value in such way like the following:

import random

def myfunction():
  return 0.1

mylist = ["apple", "banana", "cherry"]
random.shuffle(mylist, myfunction)

print(mylist) #Prints: ['banana', 'cherry', 'apple']

What will happen? From what I've seen, the way the list is organized changes. So, If I, once again, determine the value the function returns, will It be organized differently?

import random

def myfunction():
  return 0.9

mylist = ["apple", "banana", "cherry"]
random.shuffle(mylist, myfunction)

print(mylist) #Prints: ['apple', 'banana', 'cherry']

Whereas If return a value such as 0.89, the way the list is organized does not change. This leads to my doubt.

I probably made some vague assumptions, but It's the best explanation I have.




Aucun commentaire:

Enregistrer un commentaire