2

I am trying to assign two teams out of 16 teams to 8 people out of 8 people,

Here is what I have:

import random    
person = ['Ashton', 'Danny', 'Martin', 'Yves', 'Nick', 'Cormac', 'Thierry', 'Ciaran']    
team = ['France', 'Switzerland', 'England', 'Slovakia', 'Germany', 'Ukraine', 'Spain', 'Czech Republic', 'Croatia', 'Italy', 'Republic of Ireland', 'Sweden', 'Russia', 'Wales', 'Belgium']    
namesTeams = {}    
for x in person:    
    teamName = team[random.randint(0, len(team) -1)]    
    namesTeams[x] = teamName    
    team.remove(teamName)    
print(namesTeams)
Steffi Keran Rani J
  • 3,667
  • 4
  • 34
  • 56
Ashton
  • 45
  • 1
  • 8

4 Answers4

2

This can be done with random.choice([List])

Example:

import random

persons = ['Name', 'Name', 'Name', 'Name', 'Name', 'Name', 'Name', 'Name']

teams = ['France', 'Switzerland', 'England', 'Slovakia', 'Germany', 'Ukraine', 'Spain', 'Czech Republic', 'Croatia', 'Italy', 'Republic of Ireland', 'Sweeden', 'Russia', 'Wales', 'Belgium']

combinations = {p: random.choice(teams) for p in persons}

The result is a dictionary.

If you want to avoid duplicates you have to iterate over the list.

combinations = {}
for p in persons:
    team = random.choice(teams)
    combinations[p] = team
    teams.remove(team)
Erich
  • 1,838
  • 16
  • 20
1

So the thing you're trying to do is just pick random "the length of names" elements from teams. In this case, you should use random.sample():

>>> import random
>>> person = ['Name', 'Name', 'Name', 'Name', 'Name', 'Name', 'Name', 'Name']
>>> team = ['France', 'Switzerland', 'England', 'Slovakia', 'Germany', 'Ukraine', 'Spain', 'Czech Republic', 'Croatia', 'Italy', 'Republic of Ireland', 'Sweeden', 'Russia', 'Wales', 'Belgium']

>>> random.sample(team, len(person))
['Ukraine', 'Russia', 'England', 'Croatia', 'France', 'Spain', 'Italy', 'Wales']

From the documentation:

random.sample(population, k)

Return a k length list of unique elements chosen from the population sequence or set. Used for random sampling without replacement.

Returns a new list containing elements from the population while leaving the original population unchanged. The resulting list is in selection order so that all sub-slices will also be valid random samples. This allows raffle winners (the sample) to be partitioned into grand prize and second place winners (the subslices).


If you want to assign two teams per person, I'd suggest you random.shuffle() the team list, then split the list into 2 sized chunks, and put the result into a dictionary:

{person: two_teams for person, two_teams in 
                   zip(people, [team[i:i+2] for i in range(0, len(team), 2)])}
Remi Guan
  • 21,506
  • 17
  • 64
  • 87
0

Use random.choice(). In your case, you could have the following:

import random
names = ['Name1', 'Name2', 'Name3']
teams = ['France', 'Switzerland', 'England', 'Slovakia', 'Germany', 'Ukraine', 'Spain', 'Czech Republic', 'Croatia', 'Italy', 'Republic of Ireland', 'Sweeden', 'Russia', 'Wales', 'Belgium']
people = []

for name in names:
    people.append(random.choice(names), random.choice(teams))

people will be a list of tuples of a name and a team.

Raphaël Gomès
  • 938
  • 1
  • 8
  • 23
0

This creates a dictionary with names attached to teams, without duplicates

import random

person = ['Name1', 'Name2', 'Name3', 'Name4', 'Name5', 'Name6', 'Name7', 'Name8']
team = ['France', 'Switzerland', 'England', 'Slovakia', 'Germany', 'Ukraine', 'Spain', 'Czech Republic', 'Croatia', 'Italy', 'Republic of Ireland', 'Sweden', 'Russia', 'Wales', 'Belgium']
namesTeams = {}

for x in person:
    teamName = team[random.randint(0, len(team) - 1)]
    namesTeams[x] = teamName
    team.remove(teamName)

print(namesTeams)