You can simply shuffle the lists and then scroll through them with zip
(which serves to traverse several lists at the same time):
from random import shuffle
j1 = ['Jose', 'Bruno', 'Lucas', 'Eduardo', 'Pedro', 'Luciano', 'Vitor', 'Diego', 'Rômulo', '*Pisca']
j2 = ['Carlinhos', 'Carlos', 'Davi', 'Thiago', 'Paulo', 'Igor', 'Felipe', 'Marcelo', 'Matheus', 'Fabio']
j3 = ['Artur', 'Anderson', 'Gustavo', 'Rogerio', 'Marcus', 'Nando', 'Jorge', 'Rodrigo', 'Caio', 'Jonas']
shuffle(j1)
shuffle(j2)
shuffle(j3)
for n, (c1, c2, c3) in enumerate(zip(j1, j2, j3), start=1):
# imprime as outras mensagens, etc
print(f' \033[34mEQUIPE {n}\033[m')
print(f'{c1}\n{c2}\n{c3}')
I also use enumerate
to get the index at the same time I go through the lists (and I use the argument start
so that the count starts with 1, since by default it starts at zero). This is better than using a range
because it works independent of the size of the lists (assuming that they all have the same size, since zip
ends when the smallest of them ends).
Thus, with each iteration, n
is the index and c1
, c2
and c3
are one of the names of the lists j1
, j2
and j3
. And since they were scrambled earlier, I guarantee that the teams will be chosen randomly.
The problem is that shuffle
ends up modifying the list itself.
If you want to keep the original lists intact, you can use sample
, which returns another shuffled list:
from random import sample
j1 = ['Jose', 'Bruno', 'Lucas', 'Eduardo', 'Pedro', 'Luciano', 'Vitor', 'Diego', 'Rômulo', '*Pisca']
j2 = ['Carlinhos', 'Carlos', 'Davi', 'Thiago', 'Paulo', 'Igor', 'Felipe', 'Marcelo', 'Matheus', 'Fabio']
j3 = ['Artur', 'Anderson', 'Gustavo', 'Rogerio', 'Marcus', 'Nando', 'Jorge', 'Rodrigo', 'Caio', 'Jonas']
def embaralha(lista): # retorna uma cópia da lista, só que embaralhada
return sample(lista, k=len(lista))
for n, (c1, c2, c3) in enumerate(zip(embaralha(j1), embaralha(j2), embaralha(j3)), start=1):
print(f' \033[34mEQUIPE {n}\033[m')
print(f'{c1}\n{c2}\n{c3}')
Or else:
for n, (c1, c2, c3) in enumerate(zip(*map(embaralha, [j1, j2, j3])), start=1):
print(f' \033[34mEQUIPE {n}\033[m')
print(f'{c1}\n{c2}\n{c3}')
Aff, I was gonna answer that right now :( repl it. was already ready...
– Woss
@Woss I was seeing if the
itertools
There is something ready, but I didn’t find it (but maybe you can do some "magic", I don’t know).. Here is the suggestion for another possible answer :-)– hkotsubo