我正在创建一个小型的Python程序,需要为我组织的某些小组工作生成随机配对。我需要确保人员和配对不会重复出现。
这是我迄今为止所写的内容。我觉得已经很接近了,但还不知道如何解决它。
我从两个 .txt 文件中获取需要配对在一起的两个人员列表,它们可以被随机生成,没有问题。但是输出中会有重复的内容。
目前,我正在尝试创建列表并检查它们是否在该列表中,但是否有更简单的方法呢?
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)
pop()
从每个列表中删除项目,以确保没有重复吗? - Random Davis