Sean Magennis
Sean Magennis

Reputation: 43

Looking to create a team generator based on names in a list- Python

So I've been told to create two seperate football teams based on players. What I have at the moment is the list of player names and two seperate lists called team_1 and team_2 I am looking to randomly selected a player from this list and add it to team one or team two. The code at the moment adds the player to the list in team one but I need it to remove the player that has been added to team one from the player list so it can't be selected again. I was planning on checking the length of the list to see if it was empty and if so then to stop the loop. Does that make sense?

import random
team_1 = []
team_2 = []
players = ["john","garrett","aaron","mitchell"]
lenghth = len(players)
selected_team_one = random.choice(players)
team_1.append(selected_team_one)
selected_team_two = random.choice(players)
team_2.append(selected_team_two)
print(team_1)
print(team_2)

Upvotes: 1

Views: 1664

Answers (4)

iGian
iGian

Reputation: 11193

Using random.shuffle()

Let's say you have the player list:

players = [chr(element) for element in range(ord('A'), ord('Z'))]
#=> ['A', 'B', 'C', 'D', 'E', 'F', ...

And you want to build teams of five members each (need to check there are enough players):

team_members = 5

You can just shuffle the player list (maybe copy() it if you want to preserve the original) and pick the players using a range(start, stop[, step]):

shuffle(players) # <- shuffle the list 
[players[x:x+team_members] for x in range(0, len(players), 5)]

#[['P', 'B', 'G', 'L', 'N'],
# ['R', 'C', 'E', 'S', 'X'],
# ['W', 'D', 'V', 'K', 'U'],
# ['T', 'A', 'O', 'I', 'Q'],
# ['H', 'Y', 'F', 'J', 'M']]

Tip: You can also group players by role in a dict and do the same building easily teams by role.

Upvotes: 0

Vinz
Vinz

Reputation: 36

You can use 'remove' to remove the selected player after you put him in a team:

$ players.remove(selected_team_one)

Exmaple :

import random
team_1 = []
team_2 = []
players = ["john","garrett","aaron","mitchell"]
length = len(players)
while players !=[]:
    selected_player = random.choice(players)
    team_1.append(selected_player)
    players.remove(selected_player)
    
    selected_player = random.choice(players)
    team_2.append(selected_player)
    players.remove(selected_player)

print(team_1)
print(team_2)

Upvotes: 0

jacob galam
jacob galam

Reputation: 811

You have the pop method of list. It returns the last element and remove it, and if you gave it an index it returns the item in the index place and remove it from the list. You can get random index, random. Randrange (len (players)). I make it a function. Also the parameter, it a list and it by reference, that why it updates the original list (Python doesn't copy list (only if you tell it to do so))

import random

def get_random_player(players):
    return players.pop(random.randrange(len(players)))

team_1 = []
team_2 = []
players = ["john","garrett","aaron","mitchell"]

for x in range(len(players) // 2):
   team_1.append(get_random_player(players))
   team_2.append(get_random_player(players))

print(team_1)
print(team_2)

Upvotes: 1

Thierry Lathuille
Thierry Lathuille

Reputation: 24279

You could select all the unique players you need for the first team with random.sample, then build the second one by taking all of the players that aren't in the first team:

import random

players = ["john","garrett","aaron","mitchell"]
length = len(players)
team_1 = random.sample(players, k=length//2)
team_2 = [player for player in players if player not in team_1]

print(team_1, team_2)
# ['aaron', 'john'] ['garrett', 'mitchell']

Upvotes: 2

Related Questions