关于python:将16个团队中的随机团队分配给8个人中的随机人员

Assign random team out of 16 teams to a random person out of 8 people

本问题已经有最佳答案,请猛点这里访问。

我想把16个队中的两个队分配给8个人中的8个人,

以下是我的资料:

1
2
3
4
5
6
7
8
9
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)

这可以用random.choice([List])来完成。

例子:

1
2
3
4
5
6
7
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}

结果是一本字典。

如果您想避免重复,就必须遍历列表。

1
2
3
4
5
combinations = {}
for p in persons:
    team = random.choice(teams)
    combinations[p] = team
    teams.remove(team)


所以你要做的就是从teams中随机选取"names"元素的长度。在这种情况下,您应该使用random.sample()

1
2
3
4
5
6
>>> 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']

从文档中:

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).

如果你想为每个人分配两个小组,我建议你用random.shuffle()team列表,然后将列表分成两个大小的块,并将结果放入字典:

1
2
{person: two_teams for person, two_teams in
                   zip(people, [team[i:i+2] for i in range(0, len(team), 2)])}


这将创建一个带有附加到工作组的名称的字典,不重复

1
2
3
4
5
6
7
8
9
10
11
12
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)

使用random.choice()。在您的案例中,您可以有以下内容:

1
2
3
4
5
6
7
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将是一个名称和一个团队的元组列表。