1 回答
TA贡献1829条经验 获得超7个赞
我将 First_round 更改为字典以使其更直接。我还会建议一些其他的结构更改,但之后会更多。一个解决方案是简单地输入一个 if 检查,看看是否指定了特定的医院,然后如果他有更好的分数就更换医生:
def hospital_ranking_of_doctor(hospital, doctor):
return next(v for k, v in ranking_by_hospital[hospital].items() if v == doctor)
Matches = {}
Matches['Doctors'] = (doctors)
First_round = {}
# We loop through all the doctors one by one
for Doctor_ in ranking_by_doctors:
# Then we find which hospital the doctor prefers
favored_hospital = ranking_by_doctors[Doctor_].get(1.0)
# We test if that hospital has already had a doctor assigned
if favored_hospital not in First_round:
# If it has not, then we just assign the current doctor
First_round[favored_hospital] = Doctor_
else:
# If the hosptial was already assigned a doctor we compare
# that doctor with the new one and set the new one only if
# the hospital prefers him.
previously_assigned_doctor = First_round[favored_hospital]
if hospital_ranking_of_doctor(favored_hospital, previously_assigned_doctor) > hospital_ranking_of_doctor(favored_hospital, Doctor_):
First_round[favored_hospital] = Doctor_
print(First_round)
现在对于结构更改,我认为您还应该将排名结构从字典范围内的数字到医院/医生更改为只是偏好的有序列表或翻转它所以关键是医院/医生和优先级/score 是值。这将使检查医生的分数更自然,例如:
ranking_by_hospital['Hospital_2']['Doctor_2'] > ranking_by_hospital['Hospital_2']['Doctor_3']
因此,您可以将排名函数更新为:
def hospital_ranking_of_doctor(hospital, doctor):
return ranking_by_hospital[hospital][doctor]
或者简单地内联这些代码位。
添加回答
举报