给班上人进行分组,要求随机。
import random
def chouyang(a,n):
p=True
while p:
b=random.sample(a,n)
b.sort() #排序
print(b)
a=list(set(a).difference(set(b))) #去除已抽样的数据
if len(a)>0:
p=True
else:
p=False
a=list(range(1,25))
n=4
chouyang(a,n)
抽样结果:
[2, 14, 18, 23]
[10, 16, 17, 21]
[4, 13, 15, 24]
[1, 5, 11, 19]
[3, 6, 7, 9]
[8, 12, 20, 22]