I am creating a tiny little python programme that needs to generate random pairs for some group work I am organising. I need to make sure people and pairs don't appear twice.
Here is what I have written so far. I feel close but don't quite know how to fix it.
I am getting two lists of people I need to pair together from two .txt files and they are being randomly generated no problem. But I am getting repeats in the output.
I am currently going down the route of creating lists and checking if they are in that list but is there a simpler way?
import random
def split_file(file_name):
text = open(file_name)
line = text.read()
result = line.split("\n")
return result
mentors = split_file(file_name="mentors.txt")
mentees = split_file(file_name="mentees.txt")
def randomiser(group):
random_member = random.choice(group)
return random_member
pairings = []
mentees_list = []
mentors_list = []
for i in range(20):
mentee = randomiser(mentees)
if mentee not in mentees_list:
mentees_list.append(mentee)
mentor = randomiser(mentors)
if mentor not in mentors_list:
mentees_list.append(mentee)
pair = mentee + ", " + mentor
if pair not in pairings:
pairings.append(pair)
print(pair)
Aucun commentaire:
Enregistrer un commentaire