我是大壮,一个热爱探讨技术的 开发者
有时候,大家报名,用一个格式报名,对不拉。
然后这里面队伍,大家实力有高有低,个么都想和厉害的一起,咋办呢?
交给随机分组程序来吧。
分享我写的。
# -*- coding: utf-8 -*-
import random
if __name__ == "__main__":
print("--------")
# group =4
allSoccer=28
allSoccerList=[]
for i in range(allSoccer):
allSoccerList.append(i+1)
print(allSoccerList)
group1 = []
group2 = []
group3 = []
group4 = []
group_extra = []
for i in allSoccerList:
group = random.randint(1, 4)
# print(random.randint(1, 5))
if group==1:
if len(group1) != 7:
group1.append(i)
else:
# print('多出来的人:'+str(i))
group_extra.append(i)
if group==2:
if len(group2) != 7:
group2.append(i)
else:
# print('多出来的人:'+str(i))
group_extra.append(i)
if group==3:
if len(group3) != 7:
group3.append(i)
else:
# print('多出来的人:'+str(i))
group_extra.append(i)
if group==4:
if len(group4) != 7:
group4.append(i)
else:
# print('多出来的人:'+ str(i))
group_extra.append(i)
print('group_extra')
print(group_extra)
for i in group_extra:
if len(group1) != 7:
group1.append(i)
continue
if len(group2) != 7:
group2.append(i)
continue
if len(group3) != 7:
group3.append(i)
continue
if len(group4) != 7:
group4.append(i)
continue
print('group1')
print(group1)
print('group2')
print(group2)
print('group3')
print(group3)
print('group4')
print(group4)
最后打印就是随机的分组了:
--------
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28]
group_extra
[22, 23, 24, 26, 27, 28]
group1
[7, 8, 10, 14, 15, 17, 19]
group2
[5, 9, 11, 12, 13, 16, 21]
group3
[2, 25, 22, 23, 24, 26, 27]
group4
[1, 3, 4, 6, 18, 20, 28]
Process finished with exit code 0
完事儿,
这样分队,还有谁会不服咯?