我需要循环遍历并将其分配给多维列表Python

2024-09-29 19:30:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在做一个程序,将计算任何给定人数的随机群体。但是当我有很多人不容易进入的时候,让我们说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/


Tags: inforcountrangerandom团队print人数
3条回答

你的帖子有点不完整,但我认为你的要求是:

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现在包含一个列表列表,其中每个子列表是一组学生

我想你可以得到很多你想要的东西与切片一个混乱的名单。这样就避免了执行大量的pop或从列表中删除操作,这些操作的性能相对较差(不过,对于任何规模合理的组来说,性能并不重要)。整个代码需要O(N+M)时间,其中N是学生人数,M是组数(大多数其他答案是O(M*N)或更糟):

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

运行示例:

^{pr2}$

这就产生了五组四人组和两组五人组。在

这种方法可确保您始终具有相似大小的组。在

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

相关问题 更多 >

    热门问题