mercredi 21 juillet 2021

How can I attach the corresponding index to a list

I have filtered a group of students into three buckets using a for loop and by unpacking a tuple. How can I attach their corresponding student numbers to each score? Thank you.

#create index for 100 students, starting with 1
student_index = list(range(1,101))

#join index with results sheet
student_score_index = list(zip(student_index, results_sheet2))


group_a = []
group_b = []
group_c = []

# Iterate over pairs

for index, pair in enumerate(student_score_index):
    # Unpack pair: index, student_score
    index, score = pair
    # assign student scores into 3 buckets: group_a,group_b,group_c
    if score >= 60:
        group_a.append(score)
    elif score >= 50 and score <=59:
            group_b.append(score)
    else:
        group_c.append(score)

print(group_a)
[61, 67, 63, 62, 62, 62]

The desired result should be something like this for all three groups:

#print corresponding student index number, score    

group_a = [(29,61),(51,67),(63,63),(65,62),(98,62),(99,62)]

Aucun commentaire:

Enregistrer un commentaire