简体   繁体   中英

How to properly write a for loop, within another loop in Python?

I'm trying to loop an existing for loop over as many iterations as possible so that I do not have to do this manually. My nested-loop matches doctors to hospitals according to both of their preferences. Here 1.0 in the get function refers to rank 1.

This is what I came up with so far (the # in the code explains a bit more):

def hospital_ranking_of_doctor(hospital, doctor):
    return ranking_by_hospitals2[hospital][doctor]

#free_doctors is currently a range (0,10)
for i in range(len(free_doctors)):
    #Make a dictionary with name Round_(i+1) (To start at Round_1)
    Round_(str(i+1)) = {}
    #Start off with same values as last round, this action should not be performed in the first round
    Round_(str(i+1)).update(Round_(str(i))
    Round_(str(i+1))_values = list(Round_(str(i+1)).values())
    for Doctor_ in ranking_by_doctors:
        favored_hospital = ranking_by_doctors[Doctor_].get(1.0 + i) #Hospitals are ranked from 1.0 - 10.0, need 1.0 or would start at 0 and get error
        favored_hospital_doctor = Doctor_
#If the hospital and doctor have not been assigned to a match before, assign the current hospital to the current doctor
        if favored_hospital not in Round_(str(i+1)) and favored_hospital_doctor not in Round_(str(i+1))_values:
                Round_(str(i+1))[favored_hospital] = Doctor_
#If the doctor has been assigned to a match before, continue with the next doctor
    elif favored_hospital_doctor in Round_(str(i+1))_values:
        continue
#If the hospital has been assigned to a match before, check if the previously assigned doctor is ranked higher (e.g 2.0 instead of 1.0)
#When this is indeed the case, the hospital prefers the new doctor over the old doctor, so assign the new doctor as its match    
    else:
        previously_assigned_doctor = Round_(str(i+1))[favored_hospital]
        if hospital_ranking_of_doctor(favored_hospital, previously_assigned_doctor) > hospital_ranking_of_doctor(favored_hospital, Doctor_):
            Round_(str(i+1)[favored_hospital] = Doctor_
Matches['Round_'str(i+1)] = Round_(str(i+1))
Matches

free_doctors:

['Doctor_10', 'Doctor_6', 'Doctor_5', 'Doctor_9', 'Doctor_1', 'Doctor_4', 'Doctor_3', 'Doctor_7', 'Doctor_2', 'Doctor_8']

The nested for loop works, but looping over the loop gives me syntax errors. Everywhere where it says ( str(i+1) I would manually write the number before in a new command code (so 1 for round 1 with get(1.0) , and 2 for round 2 with get(2.0) . This is doable for a dataset of 10 doctors and 10 hospitals. However, I would like to increase the size of this dataset and then doing this manually becomes unsustainable. So I would like to write a loop that automatically does this for me, then the dictionary Matches should show all ten rounds with the matches attained in those rounds.

Even better than using the range(len(free_doctors)) , would be if the loop just continued until all doctors and hospitals have been matched.

It looks like you're trying to solve the "stable marriage" problem. Instead of husbands and wives being unable to "trade up", you have doctors and hospitals, but the structure is the same: students and schools, etc.

https://gist.github.com/joyrexus/9967709 is Google's first hit for "Stable Marriage" Python and it will probably do what you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM