0

我正在制作一个程序,它将为任何给定数量的人/组计算随机组。但是当我有一些不容易进入的人时,让我们说 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 或类似的东西相反。我的目标是希望随机创建团队以使其公平的教师和体育领袖。我想对班级或体育俱乐部中的人数进行随机抽样,然后将其中的 1 个添加到多维数组中的每个列表中,以便它继续循环并将它们添加到数组中,直到没有更多人为止。这样我就可以创建均匀的团队。

它应该以与此类似的方式运行...... http://chir.ag/projects/team-maker/

4

3 回答 3

1

您的帖子有点不完整,但这是我认为您要的内容:

import random

classSize = int(input('How many people are there in the class? '))
classGroup = int(input('How many groups would you like to create?' ))
groups = [[] for i in range(classGroup)]
students = list(range(classSize))

for g in range(classGroup):
    group = random.sample(students, classSize//classGroup)
    groups[g].extend(group)
    group = set(group)
    students = [s for s in students if s not in group]

for s in students:
    random.choice(groups).append(s)

groups现在包含一个列表列表,其中每个子列表是一组学生

于 2013-09-15T20:17:54.970 回答
0

I think you can get a lot of what you want with slicing of a shuffled list. This avoids doing lots of pops or removals from a list, which are relatively poor performing (though really, performance isn't likely to matter for any reasonable sized group). This whole code should take O(N+M) time where N is the number of students and M is the number of groups (most of the other answers are O(M*N) or worse):

import random

def make_groups(class_size, num_groups):
    students = list(range(class_size))
    random.shuffle(students)

    group_size = class_size // num_groups    # size of the smaller groups
    smaller_groups = num_groups - class_size % num_groups # num of smaller groups

    # start with smaller groups
    groups = [students[i:i+group_size] for i in
              range(0, smaller_groups*group_size, group_size)

    # add longer groups
    groups.extend(students[i:i+group_size+1] for i in
                  range(smaller_groups*group_size, class_size, group_size+1)

    return groups

if __name__ == "__main__":
    class_size = int(input('How many people are there in the class?'))
    num_groups = int(input('How many groups would you like to create?'))
    for group in make_groups(class_size, num_groups):
        print group

An example run:

How many people are there in the class?30
How many groups would you like to create?7
[14, 27, 18, 5]
[15, 24, 23, 13]
[22, 6, 16, 1]
[4, 29, 0, 7]
[28, 2, 12, 26]
[11, 9, 3, 17, 21]
[10, 25, 20, 19, 8]

That produces five groups of four and two groups of five.

于 2013-09-15T22:01:07.847 回答
0

这种方法可确保您始终拥有类似大小的组。

import random

class_size = input('How many people are there in the class?')
n_groups = input('How many groups would you like to create?')
people = range(class_size)

groups = [[] for i in range(n_groups)]
for i in range(class_size):
    for group in groups:
        if not people:
            break
        person = random.choice(people)
        people.remove(person)
        group.append(person)

print groups
于 2013-09-15T21:15:30.347 回答