dimanche 14 mars 2021

Equivalent result of random.sample in Python

I have a list.

top_list = [1, 4, 10]

I have also a for loop and, in the body of it, I've got:

for service in current_services:
    newNode = random.sample(top_list, 1)[0]

This returns, for each iteration, a random value (k=1)from the list until the end of the elements in top_list.

What I'm trying to do is get the same value in a sorted way and not sampling the list.

e.g. newNode = 1, newNode = 4 and newNode = 10.

The complete loop is:

for service in current_services:
            newNode = random.sample(top_list, 1)[0]
            if not self.is_already_deployed(sim, service, newNode):
                self.deploy_module(sim, service, newNode)
                logging.info(f"Adding new module of Service {service} "
                             f"to {newNode} node")

Thanks in advance.




Aucun commentaire:

Enregistrer un commentaire