简体   繁体   中英

Why variable replacement does not work in Python nested loop

I have the following code:


lower_threshold = 6.33
upper_threshold = 1e+30
threshold_dict_by_patient_and_visit = {
    "k2-01-003|15" : [15.007, 1e+30]
}
mylist = ['k2-01-003|18', 'k2-01-003|13','k2-01-003|15']

for i in range(3):
    for patient_visit in mylist:
        if (patient_visit in threshold_dict_by_patient_and_visit):
            lower_threshold, upper_threshold = threshold_dict_by_patient_and_visit[patient_visit]
        print(i, patient_visit, lower_threshold, upper_threshold)

The task I want to achieve is this:

  1. Within the range(3) loop, loop through mylist .
  2. When the content of my list exist in threshold_dict_by_patient_and_visit dictionary (in this case k2-01-003|15 ) replace the lower_threshold and upper_threshold with the value in that dictionary. Otherwise use default value: 6.33 and 1e+30

The result I expect is this:

0 k2-01-003|18 6.33 1e+30
0 k2-01-003|13 6.33 1e+30
0 k2-01-003|15 15.007 1e+30
1 k2-01-003|18 6.33 1e+30
1 k2-01-003|13 6.33 1e+30
1 k2-01-003|15 15.007 1e+30
2 k2-01-003|18 6.33 1e+30
2 k2-01-003|13 6.33 1e+30
2 k2-01-003|15 15.007 1e+30

Why it gives this instead:

0 k2-01-003|18 6.33 1e+30
0 k2-01-003|13 6.33 1e+30
0 k2-01-003|15 15.007 1e+30
1 k2-01-003|18 15.007 1e+30
1 k2-01-003|13 15.007 1e+30
1 k2-01-003|15 15.007 1e+30
2 k2-01-003|18 15.007 1e+30
2 k2-01-003|13 15.007 1e+30
2 k2-01-003|15 15.007 1e+30

What's the right way to go about it?

I'm using Python 3.8.5.


Update

I tried to add else but still doesn't work:

for i in range(3):
    for patient_visit in mylist:
        if (patient_visit in threshold_dict_by_patient_and_visit):
            lower_threshold, upper_threshold = threshold_dict_by_patient_and_visit[patient_visit]
        else:
            lower_threshold, upper_threshold = lower_threshold, upper_threshold
        print(i, patient_visit, lower_threshold, upper_threshold)
threshold_dict_by_patient_and_visit = {
    "k2-01-003|15" : (15.007, 1e+30) # using tuple instead of list because you don't need to mutate them; this is not mandatory
}
default_thresholds = (6.33, 1e+30)
mylist = ['k2-01-003|18', 'k2-01-003|13','k2-01-003|15']

for i in range(3):
    for patient_visit in mylist:
        #dict.get: get an item if it exists, otherwise return a default value
        lower_threshold, upper_threshold = threshold_dict_by_patient_and_visit.get(patient_visit, default_thresholds)
        print(i, patient_visit, lower_threshold, upper_threshold)

Here is a clean and working solution:

threshold_dict_by_patient_and_visit = {
    "k2-01-003|15" : [15.007, 1e+30]
}
mylist = ['k2-01-003|18', 'k2-01-003|13','k2-01-003|15']

for i in range(3):
    lower_threshold = 6.33
    upper_threshold = 1e+30
    
    for patient_visit in mylist:
        if patient_visit in list(threshold_dict_by_patient_and_visit.keys()):
            lower_threshold, upper_threshold = threshold_dict_by_patient_and_visit[patient_visit]
        print(i, patient_visit, lower_threshold, upper_threshold)

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