jeudi 11 juin 2020

creating a random list with specified number of random values

Is there a command to create directly a list with length 20 that contains exactly 10's 0 and 10's 1, randomly?

My code for doing that is as follows:

import random

my_list = []

for i in range(20):
    if i <= 9:
        my_list.append(0)
    else:
        my_list.append(1)

random.shuffle(my_list)

But I wonder if there is a method to do this directly?

Thanks in advance.




Aucun commentaire:

Enregistrer un commentaire