I'm new to Python, and while reading a script, I encountered the following syntax:
def approximate_random_effects(data, labels, group):
correlation_per_donor = {}
for donor_id in set(data[group]):
correlation_per_donor[donor_id], _, _, _, _ = \
linregress(
list(data[labels[0]][data[group] == donor_id]),
list(data[labels[1]][data[group] == donor_id]))
average_slope = np.array(correlation_per_donor.values()).mean()
t, p_val = ttest_1samp(correlation_per_donor.values(), 0)
Why does the LHS of correlation_per_donor[donor_id]
have _,
and why does the RHS have so many ()
with consecutive []
nested inside it? I do not understand it and even running through Codeacademy's lists/dict tutorial it did not help.
Advice is needed, thanks!
EDIT: Is my comprehension of the script correct? Attached below:
for each donor ID,
for-loop picks out donor_id in group residing in DATA:
LHS has 5 variables, but only makes donor_id
a part of a dictionary "correlation_per_donor
"
RHS does linregress
on 2 lists, with (labels[first item] in DATA, group in DATA == donor_id) and (labels[second item] in DATA, group in DATA == donor_id)