首页 > 其他 > 详细

将班级人数(比如21)随机分成三组

时间:2019-12-20 19:27:50      阅读:105      评论:0      收藏:0      [点我收藏+]
# random split a list such as the number of a class

import random 


# equal random split a list
def randomEqualSplit(mylist, n):
    resultList = [] # create result list
    part = round(len(mylist)/n) # calculate the part 
    for i in range(n): # for every part 
        extract = random.sample(mylist, part) # extrat one part 
        for i in extract : # for every element 
            if i in extract: # if the element in the list 
                mylist.remove(i) # remove the element form the list
        resultList.append(extract) # add the extract element to a empty list
    return resultList # return the list

result = randomEqualSplit([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21],3) # create the object of the function
print(result) # print the result

目标是将一个列表随机分成了3份,来源于开题时,需要将全班人数分成三组,但是谁都不想在第一组。

主要用到了一个random.sample(),其他的指示没有用到。

将班级人数(比如21)随机分成三组

原文:https://www.cnblogs.com/zijidefengge/p/12074586.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!