简体   繁体   English

如何制作所有连接回原始变量的变量“副本”

[英]How to make 'copies' of variables that all connect back to the original variable

I need to sort students into classes based on their preferences and I am planning to use the Hungarian Algorithm to sort them.我需要根据学生的喜好对他们进行分类,我打算使用匈牙利算法对他们进行分类。 The problem that I am running into though is that there are more people than classes and each class has a minimum number of people that it needs to have.我遇到的问题是人数多于班级,每个 class 都有它需要的最少人数。

In my dataset, there are ~550 students, and each one has a list of top 5 preferences.在我的数据集中,大约有 550 名学生,每个人都有一个前 5 名偏好的列表。 Every preference is an ID that corresponds to a class. Each class has a minimum and maximum capacity (in my case a min cap of 15 people and a max cap of 27 people) and there are 21 classes in the dataset.每个偏好都是一个对应于 class 的 ID。每个 class 都有最小和最大容量(在我的例子中,最小容量为 15 人,最大容量为 27 人),数据集中有 21 个类别。

Here is an example dataset for every student:这是每个学生的示例数据集:

Email Email first choice第一选择 second choice第二选择 third choice第三选择 fourth choice第四选择 fith choice第五个选择
email@gmail.com电子邮件@gmail.com 4 4个 7 7 1 1个 8 8个 21 21
email2@gmail.com email2@gmail.com 6 6个 9 9 14 14 17 17 2 2个

Here is an example dataset for every class:这是每个 class 的示例数据集:

Class Title Class 职称 Class ID Class 身份证 Min Cap最小上限 Max Cap最大上限
Class Title1 Class 标题1 1 1个 15 15 27 27
Class Title2 Class 标题2 2 2个 15 15 27 27
Class Title3 Class 标题3 3 3个 15 15 27 27

Because there are ~550 students and 21 classes and for the Hungarian algorithm to work, I was planning to make "copies" of the classes.因为有大约 550 名学生和 21 个班级,并且为了使匈牙利算法起作用,我打算制作班级的“副本”。 I would first make 15 copies of every class (like class 1.1, 1.2, 1.3, 1.4, 2.1, 2.2, 2.3, etc.) to fill the minimum requirement of the class and then would add even more copies to the most popular classes among the students until there is an equal number of students and copies of classes.我会首先为每个 class 制作 15 个副本(例如 class 1.1、1.2、1.3、1.4、2.1、2.2、2.3 等)以满足 class 的最低要求,然后将更多副本添加到最受欢迎的课程中学生,直到有相同数量的学生和班级副本。

My question is: how would I loop the copying of the variables that would, in the algorithm act like their own classes or choices for classes (since the choices would also need to be different as to put the people to different copies of the same class and not have them compete for only one variable when there are other copies of it) but then after the sorting is done, the copies could be traced back to the original?我的问题是:我将如何循环复制变量,这些变量在算法中就像它们自己的类或类的选择一样(因为选择也需要不同,以便将人们置于相同 class 的不同副本而不是让它们在有其他副本时只竞争一个变量)但是在排序完成后,这些副本可以追溯到原始版本吗?

Thank you in advance and let me know if there is anything I can clarify提前谢谢你,如果有什么我可以澄清的,请告诉我

You should know that some type s in Python are mutable types , one of them is the list type:你应该知道 Python 中的某些typemutable types ,其中之一是list类型:

>>> a = []
>>> a.append('.')
>>> a
['.']
>>> b = a
>>> b.append('$')
>>> a
['.', '$']

You may describe a list in Python like a pointer that points toward the cells.您可以将 Python 中的list描述为指向单元格的指针。


An other example may be this:另一个例子可能是这样的:

>>> def foo(n: int):
        i+=1
>>> i = 2
>>> foo(i)
>>> i
2

As you can see the i variable is not passed by reference, since it's an immutable typed variable.如您所见, i变量不是通过引用传递的,因为它是一个immutable typed变量。 But look at this:但是看看这个:

>>> def baz(n: list[int]):
        n[0]+=1
>>> i = [2]
>>> baz(i)
>>> i
[3]

The same thing is true for dict s, so that if you want to have a copy [indipendent from the original] of that dictionary you will have to do something like this: dict的情况也是如此,因此如果您想拥有该词典的副本[独立于原始版本],您将必须执行以下操作:

newDict = oldDict.copy()

So, if you need a short answer:所以,如果你需要一个简短的答案:

How to make 'copies' of variables that all connect back to the original variable?如何制作所有连接回原始变量的变量“副本”?

You just have to create them with b = a你只需要用b = a创建它们

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当工人多于工作时如何使用匈牙利算法以及如何将工作的副本链接回原始? - How to use the hungarian algorithm when there are more workers than jobs and how to link the copies of jobs back to the original? 从文件导入变量创建变量的副本 - from file import variable creates copies of variables 如何将操纵的分类变量编码回原始值? - How to encode manipulated categorical variable back to original values? 如何制作Cython对象的副本? - how to make copies of Cython objects? 如何在TensorFlow中创建变量/常量/占位符的副本? - How to create copies of variables / constants / placeholders in TensorFlow? 如何创建未链接到原始对象的python对象的副本? - How to create copies of a python object that are not linked to original object? 如何将 sklearn 预处理交互变量的输出连接回原始数据帧? - How to join output from sklearn preprocessing interaction variables back to original dataframe? 如何在Python中使所有引用变量都为None? - How to make all the reference variables None in Python? 如何仅在 1 个变量与所有其他变量之间创建交互变量 - How to create interaction variable between only 1 variable with all other variables Python-迭代完成后,变量将恢复为原始值 - Python - Variables snap back to original values when iteration is finished
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM