-2

I want to write a program to partition a list of names equally into a dictionary of names and their teams and the problem is I don't know how to distribute team_list to players

import random
team_list = ['A', 'B']
players = ['Hosein', 'Maziar', 'Akbar', 'Nima', 'Mehdi', 'Farhad', 'Mohamad', 'Khashayar', 'Milad', 'Mostafa',
                         'Amin', 'Saeed', 'Pooya', 'Pooria', 'Reza', 'Ali', 'Behzad', 'Soheil', 'Behrooz', 'Shahrooz', 'Saman', 'Mohsen']

def team(player_list):
    random.shuffle(player_list)
    return [player_list[i::2] for i in range(2)]

print(team(players))
9
  • 2
    First, you need to explain how you will represent a team in your program, and then you need to explain what it means to put a player on that team. But the code you wrote to describe Human already doesn't make any sense. I think you need to follow a tutorial more closely, and try to think more carefully about your logic. Feb 11, 2021 at 11:58
  • I want to print names and their teams , and teams have no attributes at all
    – Xus
    Feb 11, 2021 at 12:00
  • Could start by actually making a dictionary in the first place, have teams as keys and keep a list of players as values which you could fill by an iteration. Feb 11, 2021 at 12:19
  • maybe teams = dict() teams["A"] = player_list[0::2] teams["B"] = player_list[1::2] ?
    – furas
    Feb 11, 2021 at 15:26
  • 1
    I don't understand what you want - in my answer program assigns names to teams and teams to names automatically.
    – furas
    Feb 11, 2021 at 16:20

1 Answer 1

1

I don't know what result you expect but I see two posible versions

Dictionary with teams

teams = dict()

teams["A"] = player_list[0::2]
teams["B"] = player_list[1::2]

for team, names in teams.items():
    print(team, names)

Result

A ['Nima', 'Shahrooz', 'Pooya', 'Mehdi', 'Behzad', 'Maziar', 'Saeed', 'Mostafa', 'Akbar', 'Reza', 'Behrooz']
B ['Ali', 'Mohamad', 'Pooria', 'Milad', 'Hosein', 'Saman', 'Farhad', 'Mohsen', 'Soheil', 'Amin', 'Khashayar']

Dictionary with players

players = dict()

for i, team in enumerate(team_list):
    for name in player_list[i::2]:
        players[name] = team

Result:

names as assigned to teams randomly but dictionary keeps them in order: first all random names for "A" and later all random names for "B"

Nima A
Shahrooz A
Pooya A
Mehdi A
Behzad A
Maziar A
Saeed A
Mostafa A
Akbar A
Reza A
Behrooz A
Ali B
Mohamad B
Pooria B
Milad B
Hosein B
Saman B
Farhad B
Mohsen B
Soheil B
Amin B
Khashayar B

Full working code:

import random

team_list   = ['A', 'B']

player_list = ['Hosein', 'Maziar', 'Akbar', 'Nima', 'Mehdi', 
               'Farhad', 'Mohamad', 'Khashayar', 'Milad', 'Mostafa', 
               'Amin', 'Saeed', 'Pooya', 'Pooria', 'Reza', 
               'Ali', 'Behzad', 'Soheil', 'Behrooz', 'Shahrooz', 
               'Saman', 'Mohsen']

random.shuffle(player_list)

print('--- teams ---')

teams = dict()

teams["A"] = player_list[0::2]
teams["B"] = player_list[1::2]

#print(teams)

for team, names in teams.items():
    print(team, names)
    
print('--- players ---')

players = dict()

for i, team in enumerate(team_list):
    for name in player_list[i::2]:
        players[name] = team

#print(players)

for name, team in players.items():
    print(name, team)

EDIT:

I added code which change order in dictionary to keep original order. But dictionary doesn't have to keep order so I don't know if it makes sense.

I created also list which keeps items [name, team] with original order but list is not so useful like dictionary.

import random

team_list   = ['A', 'B']

player_list = ['Hosein', 'Maziar', 'Akbar', 'Nima', 'Mehdi', 
               'Farhad', 'Mohamad', 'Khashayar', 'Milad', 'Mostafa', 
               'Amin', 'Saeed', 'Pooya', 'Pooria', 'Reza', 
               'Ali', 'Behzad', 'Soheil', 'Behrooz', 'Shahrooz', 
               'Saman', 'Mohsen']

player_list_random = player_list.copy()   # duplicate to keep original `player_list`
random.shuffle(player_list_random)

print('--- teams ---')

teams = dict()

teams["A"] = player_list_random[0::2]
teams["B"] = player_list_random[1::2]

#print(teams)

for team, names in teams.items():
    print(team, names)
     
print('--- players ---')

players = dict()

for i, team in enumerate(team_list):
    for name in player_list_random[i::2]:
        players[name] = team

players_in_order_dict = dict()
players_in_order_list = list()

# uses original `player_list` to create dict with original order
for name in player_list:
    players_in_order_dict[name] = players[name]
    players_in_order_list.append( [name, players[name]] )
#print(players)

print('- dict -')
for name, team in players_in_order_dict.items():
    print(name, team)

print('- list -')
for name, team in players_in_order_list:
    print(name, team)
11
  • I want to generate teams randomly from the list of names
    – Xus
    Feb 11, 2021 at 15:47
  • they are random - I use random.shuffle(player_list). But in dictionary I keep then in order: first random names for A, next random names for B
    – furas
    Feb 11, 2021 at 15:56
  • if you want to keep original order from list player_list then you will have to use my dictionary with with original player_list to create dictionary with different order. But I don't know if this makes send.
    – furas
    Feb 11, 2021 at 16:03
  • yes , but I want the distribution full randomly and automatically and I dont know how to do that
    – Xus
    Feb 11, 2021 at 16:06
  • the order doesn't matter as well as the form of output , the only thing that matters is being generated randomly and equally
    – Xus
    Feb 11, 2021 at 16:09

Not the answer you're looking for? Browse other questions tagged or ask your own question.