【发布时间】:2017-10-16 13:36:36
【问题描述】:
我正在尝试创建一个程序,该程序可以从班级生成学生组,但不会创建以前创建的组。具体来说,我需要每周从同一组学生中创建 2 人的新学生实验组,并且我尽量不要将相同的两个学生配对超过一次。在前几周配对的学生将以某种方式作为输入。
过去的组也需要排除它们的镜像,即如果[1,2]是过去的组,[2,1]也是过去的组。
我的程序在下面。它解决了这个问题,但我想它的效率非常低。如果它是一个更好的解决方案,我会接受完全不同的代码。
import numpy,random
from itertools import combinations
class_list="""a\nb\nc\nd\ne\nf\ng\nh\ni\nj\nk\nl\nm\nn\no\np
"""
students=class_list.splitlines()
#print len(students),students
combs=[map(int, comb) for comb in combinations(range(len(students)), 2)]
#print combs
done_list=[[0,4],[1,6],[2,13],[3,12],[8,10],[11,14],[15,9],
[0,13],[1,4],[2,7],[3,12],[5,6],[8,10],[14,15],
[0,1],[2,3],[4,5],[6,7],[8,9],[10,11],[12,15],[13,14],
[0,2],[1,3],[4,6],[5,7],[8,14],[10,9],[12,11],[15,13]]
for i_done in done_list:
if i_done in combs:
combs.remove(i_done)
f_done=False
while(1):
if f_done:
break
final_list=[]
final_list_used_students=[]
for _i in range(len(students)/2):
rand_i=random.randint(0,len(combs)-1)
if combs[rand_i][0] not in final_list_used_students and combs[rand_i][1] not in final_list_used_students:
final_list.append(combs[rand_i])
final_list_used_students.append(combs[rand_i][0])
final_list_used_students.append(combs[rand_i][1])
if len(final_list_used_students)==len(students):
f_done=True
break
print final_list
【问题讨论】:
-
什么组? 2 种组合?
-
为什么这个人为的代码,以及你为什么使用 Python 2 - 对此有特殊要求吗?
-
做作,因为这是我的要求,如果可以轻松修改以生成解决方案,请尝试给出我的示例
-
那我建议你马上切换——Python 3 是新手的首选语言。无论如何,你真的需要随机 - 为什么不订购?
标签: python combinations