0

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)
3
  • Add an else where you reset to the default values for the threshold variables? Commented Oct 19, 2020 at 7:39
  • @Someprogrammerdude I tried. It doesn't work. see my update above. Commented Oct 19, 2020 at 7:40
  • With lower_threshold, upper_threshold = lower_threshold, upper_threshold you just assign the same values as the variables already have. You need to assign the default value, explicitly. Commented Oct 19, 2020 at 7:42

2 Answers 2

1
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)
Sign up to request clarification or add additional context in comments.

Comments

0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.