Keaton Garner
Keaton Garner

Reputation: 11

How do I generate multiple random lists for a portion of a larger list in Python? I keep getting duplicate lists

Started working with Python today and decided to test my learnings on a small project.

I want to have a "roster" of, lets say 12 people. From this roster, I want there to be "x" number of teams of "y", depending on how many people want to play. I know how to write this, if we assume all names in the roster are going to be used, but where I'm lost is how to generate unique teams for a composition of something like 3 teams of 2. Whenever I try to solve this, I will get 3 of the same teams of 2.

Here is an example of the code I'm attempting

import random

names = ["Tom", "Sam", "Jake", "McKenzie", "Sarah", "Devon", "Brice", "Caleb", "Mitchell", "Scott", "Nick", "Liz"]

teams = int(input("How many teams will there be? "))
players = int(input("How many players will each team have? "))

remaining_teams = teams
item = random.sample(names, k=players)


while remaining_teams > 0:
    print(item)
    remaining_teams = remaining_teams - 1
    if remaining_teams < 1:
        break

Right now, if my input variables were 4 and 2, I would get something returning like:

["Sam", "Tom"]
["Sam", "Tom"] 
["Sam", "Tom"] 
["Sam", "Tom"] 

How can I change this to get random results closer to this?:

["Sam", "Tom"]
["Nick", "Sarah"]
["Devon", "Jake"]
["Mitchell, "Liz"]

Upvotes: 1

Views: 568

Answers (3)

inordirection
inordirection

Reputation: 987

You just aren't ever re-sampling the names to construct a new team. When you have item = random.sample(names, k=players) outside the loop, that code is only going to run once, not every time you subsequently reference/print item. You should put it in the loop so that every team is a new sample of names. Also, you might want to remove names from the names list once they're added to a team (so players can't show up on multiple teams). You could do this with another simple for loop inside your while loop. Finally, the last break statement is unnecessary: the while loop will automatically stop when remaining_teams hits 0, so no need to check and break if its less than 1.

You could also check to make sure you have enough names to fill the teams you request (so that you don't have a runtime error if, for instance, 3 teams of 15 players are requested, but you only have 15 names).

Here are all those changes altogether:

import random

names = ["Tom", "Sam", "Jake", "McKenzie", "Sarah", "Devon", "Brice", "Caleb", "Mitchell", "Scott", "Nick", "Liz"]

teams = int(input("How many teams will there be? "))
players = int(input("How many players will each team have? "))

if teams * players > len(names):
  print("Not enough players")
  exit()

remaining_teams = teams

while remaining_teams > 0:
    team = random.sample(names, k=players)
    print(team)
    for player in team:
      names.remove(player)
    remaining_teams = remaining_teams - 1

Upvotes: 0

ezzeddin
ezzeddin

Reputation: 521

You can also just use a for loop:

import random

names = ["Tom", "Sam", "Jake", "McKenzie", "Sarah", "Devon", "Brice", "Caleb", "Mitchell", "Scott", "Nick", "Liz"]

teams = 4 # int(input("How many teams will there be? "))
players = 2 # int(input("How many players will each team have? "))

for i in range(teams):
    item = random.sample(names, k=players)
    names.remove(item[0])
    names.remove(item[1])
    print(item)

Upvotes: 0

Bashi
Bashi

Reputation: 174

Try to put this line

item = random.sample(names, k=players)

inside the while loop:

while remaining_teams > 0:
    item = random.sample(names, k=players)
    print(item)
    remaining_teams = remaining_teams - 1

There is no need to check if remaining_teams is less than 0 because if it is zero it will never enter the loop so it won't get lower.

Upvotes: 1

Related Questions