samedi 11 décembre 2021

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"]



Aucun commentaire:

Enregistrer un commentaire