简体   繁体   中英

How to pick largest element from list?

I wrote a program to count grades and the students' names and I want to implement a feature that shows the highest grade and the respective student. The whole program works except for this part, since I'm not sure how to pick the element with the highest value since it comes with a string attached.

I'll leave the entire program down but the part that I'm refering to is this:

mediafinal = []


    elif resposta.upper() == 'N':
        resposta = input('Check best student and grade? (Y/N):')
        if resposta.upper() == 'Y':
            print('Best student and grade is: ', max(mediafinal))
            break

The rest of the code

media1 = []
media2 = []
media3 = []
mediafinal = []
nomes = []

while True:
    resposta = input('Pretende introduzir um aluno? (Y/N): ')
    if resposta.upper() == 'Y':
        nome = (input('Indique o nome do aluno: '))
        nomes.append(nome)
        nota1 = eval((input(f'Indique a nota do primeiro teste {nome}: ')))
        nota2 = eval((input(f'Indique a nota do segundo teste {nome}: ')))
        m1 = ((nota1 + nota2)/2)
        infoaluno1 = []
        infoaluno1.extend([nome, m1])
        media1.append(infoaluno1)
        print(f'O nome e média do primeiro período do aluno é: {nome} {m1}')

        nota3 = eval((input(f'Indique a nota do terceiro teste {nome}: ')))
        nota4 = eval((input(f'Indique a nota do quarto teste {nome}: ')))
        m2 = ((nota3 + nota4) / 2)
        infoaluno2 = []
        infoaluno2.extend([nome, m2])
        media2.append(infoaluno2)
        print(f'O nome e média do segundo período do aluno é: {nome} {m2}')

        nota5 = eval((input(f'Indique a nota do quinto teste {nome}: ')))
        nota6 = eval((input(f'Indique a nota do sexto teste {nome}: ')))
        m3 = ((nota5 + nota6) / 2)
        infoaluno3 = []
        infoaluno3.extend([nome, m3])
        media3.append(infoaluno3)
        print(f'O nome e média do terceiro período do aluno é: {nome} {m3}')

        mf = ((m1 + m2 + m3)/3)
        mediafinal.extend([nome, mf])
        print(f'Classificação final do aluno: {nome} {mf}')

    elif resposta.upper() == 'N':
        resposta = input('Pretende verificar o melhor aluno e nota? (Y/N):')
        if resposta.upper() == 'Y':
            print('O melhor aluno é:', max(mediafinal))
            break

        elif resposta.upper() == 'N':
            print('Ok!')
            break

The problem is much much MUCH simpler if you have a list of tuples instead of alternating strings and numbers:

mediafinal.extend([nome, mf])

should be:

mediafinal.append((mf, nome))

Then your max call just works out of the box like this:

mf, nome = max(mediafinal)
print('Best student and grade is: ', nome, mf)

Note that putting the score ( mf ) first in the tuple means that max will pick based on that. You could change the ordering, but then you need to tell max to make its selection based on the 2nd element:

mediafinal.append((nome, mf))
...
nome, mf = max(mediafinal, key=lambda m: m[1])

So without changing any of your code logic, and just fixing the max() line, I would have it as:

max(a, key=lambda el: el[1])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM