Let's start with a simple task. Suppose we have heartrate data that's organized as follows:
#in row i we have data for setting i
#in column j we have data for subject j
hr = [ [ 72, 75, 71, 73, 0], # resting
[ 91, 90, 94, 93, 0], # walking slowly
[ 130, 135, 139, 142, 0], # running on treadmill
[ 120, 118, 110, 105, 0]] # after minute recovery
Suppose we want to get the average for every subject. (That is, we want the average for each column.) First, let's write a function that computes the average for column col_ind
.
def get_col_avg(data, col_ind):
'''Return the average of column col_ind in a the
nested list data'''
s = 0
for setting in data:
s += setting[col_ind]
return s/len(data)
Now, we can get the average for each subject/column using a for-loop:
col_avgs = []
for col_ind in range(len(hr[0])):
col_avgs.append(get_col_avg(hr, col_ind))
Here's a different approach: make a nested loop -- a loop within a loop
subj_avgs = []
for subject_ind in range(len(hr[0])):
s = 0
for setting_ind in range(len(hr)):
s += hr[setting_ind][subject_ind]
subj_avgs.append(s/len(hr))
from IPython.display import YouTubeVideo
YouTubeVideo("7i4KTI6QOfA")
Here is something that is more directly analguous to the approach we took when we called get_call_avg
:
subj_avgs = []
for subject_ind in range(len(hr[0])):
s = 0
for setting in hr:
s += setting[subject_ind]
subj_avgs.append(s/len(hr))
As you can see, nested loops -- loops within loops -- can be avoided by instead calling functions (which contain loops) from within loops.