I am still learning Python and I have been trying to sort this problem for hours but somehow cannot get it working: Suppose we have a list of tuples containing names and grades:
[("Mark", [5,4,1,2,4,3]), ("John", [1,2,3,1,2,1]), ("Patrick", [1,3,5,1,2,4])]
And we want to get this output:
[("John", [1,2,3,1,2,1]), ("Patrick", [1,3,5,1,2,4]), ("Mark", [5,4,1,2,4,3])]
, where the original list is is sorted in ascending order of students' marks score (suppose we get marks score by discarding the highest mark and adding all remaining marks together). I.e. John has marks 1,2,3,1,2,1, so we discard mark 3 and add 1+2+1+2+1 together, which gives us 7.
This task wouldnt be problem at all, if I wasnt required to get that output with all their original marks. I wrote this function:
def sortedFinalRes(input):
finalRes = 0
sortedRes = []
b = []
for i in range(0, len(input)):
total = 0
a = sorted(input[i][1], key = lambda x: x)
a.pop()
sortedRes += input[i][0], a
total = sum(sortedRes[i*2+1])
b += (sortedRes[i*2], total)
c = sorted(b, key = lambda x: str(x))
print(sortedRes)
print(b)
, where I basically at first sort marks of each student so that I can then discard the highest one by using pop() and then I sum all remaining ones, which gives me marks score. Then I sort it according to marks score, but the output is required to be sorted, but with original marks, not with the mark score. Any ideas appreciated! Cheers