我正在做一个程序,将计算任何给定人数的随机群体。但是当我有很多人不容易进入的时候,让我们说6个。然后我想循环向每个列表中添加一个人,直到我没有更多要添加的人。在
import random
classSize = int(input('How many people are there in the class?'))
classGroup = int(input('How many groups would you like to create?'))
classGroupNumber = classSize // classGroup
classGroupRemainder = classSize % classGroup
print(classGroupNumber)
print(classGroupRemainder)
count = 0
if classGroupRemainder == 0:
randomClass = random.sample(range(1,classSize+1),classSize)
for t in range(1,classGroup+1):
print('\n')
for i in range(0,classGroupNumber):
count += 1
print('group',t,'- Student - ',randomClass[count-1])
else:
iterator = 0
randomClass = random.sample(range(1,classSize+1),classSize)
newList = [[] for x in range(classGroup)]
while iterator < classSize:
for i in range(0,1):
newList[i].append(randomClass[iterator])
iterator += 1
print(newList)
print(iterator)
print(randomClass)
基本上说我有15个人。我想把这些人分成小组或小组。但我想随机进行,我希望有公平的团队。所以如果我有15个人,我想要4个团队。我会有3组4人和1组3人。而不是5,4,3,3或类似的东西。我的目标是教师和体育领导人,他们想随机创建团队,使之公平。我想从班级或体育俱乐部的人数中随机抽取一个样本,然后在多维数组中的每个列表中添加一个,这样它就可以不断循环并将他们添加到数组中,直到没有更多的人为止。这样我就能创造出一个均衡的团队。在
它的功能应该与此类似。。。。http://chir.ag/projects/team-maker/
你的帖子有点不完整,但我认为你的要求是:
groups
现在包含一个列表列表,其中每个子列表是一组学生我想你可以得到很多你想要的东西与切片一个混乱的名单。这样就避免了执行大量的pop或从列表中删除操作,这些操作的性能相对较差(不过,对于任何规模合理的组来说,性能并不重要)。整个代码需要
O(N+M)
时间,其中N
是学生人数,M
是组数(大多数其他答案是O(M*N)
或更糟):运行示例:
^{pr2}$这就产生了五组四人组和两组五人组。在
这种方法可确保您始终具有相似大小的组。在
相关问题 更多 >
编程相关推荐