How do I draw random names from each list without repeating them

Asked

Viewed 584 times

4

I want to draw random names from 3 different lists, I have a program in Python that does more or less that. The problem is that they keep repeating several names in different combinations, I would like the program to take only one name from each list and form a team.

from random import choice
from time import sleep

lista = []

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']

for c in range(1, 11):
    print('PROCESSANDO.....')
    sleep(2)
    print('-' * 20)
    print(f'      \033[34mEQUIPE {c}\033[m')
    print('-' * 20)
    sorteio = (f'{choice(j1)}\n'
               f'{choice(j2)}\n'
               f'{choice(j3)}')
    print(sorteio)
    print('-' * 20) 

2 answers

7

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}')
  • 4

    Aff, I was gonna answer that right now :( repl it. was already ready...

  • 1

    @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 :-)

3

You can remove the drawn item from the list. See below:

from random import choice
from time import sleep

lista = []

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']

for c in range(1, 11):
    print('PROCESSANDO.....')
    sleep(2)
    print('-' * 20)
    print(f'      \033[34mEQUIPE {c}\033[m')
    print('-' * 20)
    #
    j1_player = choice(j1)
    j1.remove(j1_player)
    j2_player = choice(j2)
    j2.remove(j2_player)
    j3_player = choice(j3)
    j3.remove(j3_player)
    #
    sorteio = (f'{j1_player}\n'
               f'{j2_player}\n'
               f'{j3_player}')
    print(sorteio)
    print('-' * 20) 

Output example

PROCESSANDO.....
--------------------
      EQUIPE 1
--------------------
*Pisca
Paulo
Caio
--------------------
PROCESSANDO.....
--------------------
      EQUIPE 2
--------------------
Pedro
Igor
Jorge
--------------------
PROCESSANDO.....
--------------------
      EQUIPE 3
--------------------
Diego
Carlos
Artur
--------------------
PROCESSANDO.....
--------------------
      EQUIPE 4
--------------------
Vitor
Felipe
Jonas
--------------------
PROCESSANDO.....
--------------------
      EQUIPE 5
--------------------
Luciano
Fabio
Anderson
--------------------
PROCESSANDO.....
--------------------
      EQUIPE 6
--------------------
Rômulo
Carlinhos
Nando
--------------------
PROCESSANDO.....
--------------------
      EQUIPE 7
--------------------
Lucas
Thiago
Rodrigo
--------------------
PROCESSANDO.....
--------------------
      EQUIPE 8
--------------------
Jose
Marcelo
Gustavo
--------------------
PROCESSANDO.....
--------------------
      EQUIPE 9
--------------------
Bruno
Davi
Rogerio
--------------------
PROCESSANDO.....
--------------------
      EQUIPE 10
--------------------
Eduardo
Matheus
Marcus
--------------------

UPDATE

If all names can be in a single list, there is another (simpler) way to do it, see below:

>>> j = ['Jose', 'Bruno', 'Lucas', 'Eduardo', 'Pedro', 'Luciano', 'Vitor', 'Diego', 'Rômulo', '*Pisca', 'Carlinhos', 'Carlos', 'Davi', 'Thiago', 'Paulo', 'Igor', 'Felipe', 'Marcelo', 'Matheus', 'Fabio', 'Artur', 'Anderson', 'Gustavo', 'Rogerio', 'Marcus', 'Nando', 'Jorge', 'Rodrigo', 'Caio', 'Jonas']
>>>
>>> while len(j) != 0:
...     random.shuffle(j)
...     print(j[:3])
...     j = j[3:]
...
['Rômulo', 'Rodrigo', '*Pisca']
['Luciano', 'Nando', 'Carlos']
['Jonas', 'Eduardo', 'Igor']
['Fabio', 'Davi', 'Lucas']
['Thiago', 'Marcus', 'Paulo']
['Vitor', 'Felipe', 'Bruno']
['Anderson', 'Pedro', 'Marcelo']
['Caio', 'Artur', 'Matheus']
['Rogerio', 'Diego', 'Carlinhos']
['Jose', 'Jorge', 'Gustavo']
>>>

I hope it helps

Browser other questions tagged

You are not signed in. Login or sign up in order to post.