我希望我的功能可以根据给定的大小(Z)将学生的随机列表分成几组。学生(X,Y)是一个功能,可以删除组X中同样属于组Y的所有学生,但这对于这个问题并不重要。
给定的输入可以是random_teams(S,T,[6,5,4]) 使用此函数,我想创建大小为6,5和4的组。因此,我想迭代此列表。然而,它只返回一个包含6个元素的列表,并没有给我一个包含剩余5和4的列表。
def random_teams(X,Y,Z):
l = random.sample(student(X,Y), len(student(X,Y)))
for i in Z:
return l[:i]
l = l[i:]
答案 0 :(得分:0)
这样的事情会起作用:
from random import shuffle
#Assuming X and Y each to be a list of students
def random_teams(X,Y,Z):
#call your student() function here, and return 2 lists, X and Y
#X,Y = student(X,Y)
allStudents = X + Y
numStudents = sum(Z)
assert len(allStudents) == numStudents, "Mismatch in number of students"
shuffle(allStudents) # remove this line if you dont want random ordering
groups = []
for size in Z:
tempGroup = []
for i in range(size):
tempGroup.append(allStudents.pop())
groups.append(tempGroup)
random_teams(["mary", "john", "jason"],["ali", "simon","jane","james"],[1,3,3])
这将给出以下输出:
[['jason'], ['james', 'simon', 'john'], ['mary', 'ali', 'jane']]