santidecali
santidecali

Reputation: 7

How do I assign elements from 1 list to elements of another list randomly?

What I am trying to do is randomly assign each baseball player to a position, and generate a list of a complete team. Any idea how I can get this to work? Thank you!

This is the code I have right now:

#list of positions on the baseball diamond
positions = ['P','C','1B','2B','3B','SS','LF','CF','RF' ]
#list of players
players = ["Alex", "Zach", "Christian", "Kelso", "Ethan", "Nick", "Marbelk", "Santi", "Greg"]
team = list(zip(positions, players))
print(team)

Upvotes: 0

Views: 64

Answers (1)

Connor Forbes
Connor Forbes

Reputation: 124

I think the easiest way about this would be to shuffle one of the lists before performing the zip operation.

For example:

import random

positions = ['P','C','1B','2B','3B','SS','LF','CF','RF' ]
players = ["Alex", "Zach", "Christian", "Kelso", "Ethan", "Nick", "Marbelk", "Santi", "Greg"]
random.shuffle(players)
team = list(zip(positions, players))

Upvotes: 2

Related Questions