I am in a beginning coding class and I can not seem to turn the basics I'm taught into a working program with a list this complicated. What functions should I be using to do this?
At this point we have not discussed importing any extra features (numpy etc) and I know people use lambda a lot (though I don't really understand what it does), but that has not been introduced in this class.
#This is an example of the structure of a student dictionary
#They have an id number
#They have a first name, last name and a list of assignments
#Assignments are tuples of an assignment name and grade
#The grade is a 4 point scale from 0 to 4
'''
student_list = [{'id': 12341, 'first_name': 'Alice', 'last_name': 'Anderson',
'assignments': [('assignment_1', 0), ('assignment_2', 2), ('assignment_3', 4)]},
{'id': 12342, 'first_name': 'Boris', 'last_name': 'Bank',
'assignments': [('assignment_1', 1), ('assignment_2', 3), ('assignment_3', 0)]},
{'id': 12343, 'first_name': 'Carl', 'last_name': 'Cape',
'assignments': [('assignment_1', 2), ('assignment_2', 4), ('assignment_3', 1)]},
{'id': 12344, 'first_name': 'Didi', 'last_name': 'Dawson',
'assignments': [('assignment_1', 3), ('assignment_2', 0), ('assignment_3', 2)]},
{'id': 12345, 'first_name': 'Ed', 'last_name': 'Enders',
'assignments': [('assignment_1', 4), ('assignment_2', 1), ('assignment_3', 3)]}]
#This function should return a list of the n student dictionaries with the
#highest grades on the assignment passed in as assignment name
#If there is a tie then it is broken by returning the student(s) with the
#lowest id number(s)
def highest_n_grades(students, assignment_name, n):
Edit
Sorry, I'm not trying to get an answer. I see how that looks. I feel like I've written out and deleted a million things and that's my problem. I'm having trouble even getting started.
I was hoping for a point in the right direction in terms of maybe what commands can grab highest grades etc. all I really have so far is something like:
def highest_n_grades(student_list):
for s in student_list:
for assignment_name, grade in s['assignments']:
if int(grade) >= 4:
print(assignment_name, grade)
highest_n_grades(student_list)
But I know that's not even really getting me started. It doesn't have three inputs and it's not looking for the max, it's looking for the manually entered value 4, and it's not even coming close to tying at back to student names or making another list.
Edit 2
Also tried that gave an error I was trying to sort the dictionary rather than the list.
def highest_n_grades(student_list, assignment_name):
for s in student_list:
for assignment_name in s['assignments'][1]:
s['assignments'][1] = assignment_name
s.sort(key=assignment_name)
print(student_list)
highest_n_grades(student_list, assignment_name='assignment_1' )
Edit 3
OK, I've maybe made a little headway?
newlist2 = sorted(newlist, key=lambda k: k['assignments'][0], reverse = True)
newlist3 = sorted(newlist, key=lambda k: k['assignments'][1], reverse = True)
newlist4 = sorted(newlist, key=lambda k: k['assignments'][2], reverse = True)
These seem to be sorting by assignment. I don't understand what lambda is doing, but I at least can generate a list with the highest grade coming up first. I think that's a baby step.
Edit 4
Here is a function I created. It seems to get me what I want, it outputs the highest 3 students, but it prints it 5 times? and I know this isn't really flexible but it's a start.
def highest_n_grades(student_list, n):
for s in student_list:
newlist = sorted(student_list, key=lambda k: k['assignments'][0], reverse=True)
print(newlist[:n])
highest_n_grades(student_list, 3)
output:
[{'id': 12345, 'first_name': 'Ed', 'last_name': 'Enders', 'assignments': [('assignment_1', 4), ('assignment_2', 1), ('assignment_3', 3)]}, {'id': 12344, 'first_name': 'Didi', 'last_name': 'Dawson', 'assignments': [('assignment_1', 3), ('assignment_2', 0), ('assignment_3', 2)]}, {'id': 12343, 'first_name': 'Carl', 'last_name': 'Cape', 'assignments': [('assignment_1', 2), ('assignment_2', 4), ('assignment_3', 1)]}]
[{'id': 12345, 'first_name': 'Ed', 'last_name': 'Enders', 'assignments': [('assignment_1', 4), ('assignment_2', 1), ('assignment_3', 3)]}, {'id': 12344, 'first_name': 'Didi', 'last_name': 'Dawson', 'assignments': [('assignment_1', 3), ('assignment_2', 0), ('assignment_3', 2)]}, {'id': 12343, 'first_name': 'Carl', 'last_name': 'Cape', 'assignments': [('assignment_1', 2), ('assignment_2', 4), ('assignment_3', 1)]}]
[{'id': 12345, 'first_name': 'Ed', 'last_name': 'Enders', 'assignments': [('assignment_1', 4), ('assignment_2', 1), ('assignment_3', 3)]}, {'id': 12344, 'first_name': 'Didi', 'last_name': 'Dawson', 'assignments': [('assignment_1', 3), ('assignment_2', 0), ('assignment_3', 2)]}, {'id': 12343, 'first_name': 'Carl', 'last_name': 'Cape', 'assignments': [('assignment_1', 2), ('assignment_2', 4), ('assignment_3', 1)]}]
[{'id': 12345, 'first_name': 'Ed', 'last_name': 'Enders', 'assignments': [('assignment_1', 4), ('assignment_2', 1), ('assignment_3', 3)]}, {'id': 12344, 'first_name': 'Didi', 'last_name': 'Dawson', 'assignments': [('assignment_1', 3), ('assignment_2', 0), ('assignment_3', 2)]}, {'id': 12343, 'first_name': 'Carl', 'last_name': 'Cape', 'assignments': [('assignment_1', 2), ('assignment_2', 4), ('assignment_3', 1)]}]
[{'id': 12345, 'first_name': 'Ed', 'last_name': 'Enders', 'assignments': [('assignment_1', 4), ('assignment_2', 1), ('assignment_3', 3)]}, {'id': 12344, 'first_name': 'Didi', 'last_name': 'Dawson', 'assignments': [('assignment_1', 3), ('assignment_2', 0), ('assignment_3', 2)]}, {'id': 12343, 'first_name': 'Carl', 'last_name': 'Cape', 'assignments': [('assignment_1', 2), ('assignment_2', 4), ('assignment_3', 1)]}]