How to relate two lists, getting the value of one through the maximum of the other?

0

I am stuck in an exercise that asks for an X number of teams participating in a championship. In the later lines the team name and its score are entered. On the way out, the team that scored the most and the average.

My current code is:

numero_de_times = int(raw_input())
gols = []
times = []
media1 = 0

for numeros in range(numero_de_times):
 time = str(raw_input())
 gol=  float(raw_input())
 media1 = media1 + gol
 media2 = media1 / numero_de_times

 times.append(time)
 gols.append(gol)


print "Time(s) com melhor ataque (%d gol(s)):" % max(gols)
print "%s" % 
print "Média de gols marcados: %.1f" % media2

As you can see I have created an empty list to keep goals and teams. If it was just to know the highest score I would have finished. But I have a hard time returning the team name and their score. Can anyone help me?

    
asked by anonymous 24.10.2018 / 21:09

2 answers

5

There is no need to store all teams and their number of goals - just store one: the largest you've seen so far - if, during the run, you find a larger number of goals than you had seen before, switch the contents of your variable and start storing the new champion. In the end, the champion will be the team that has left over in the variable.

numero_de_times = int(raw_input())
soma = 0
maior = 0

for numeros in range(numero_de_times):
 time = str(raw_input())
 gol=  float(raw_input())
 soma = soma + gol
 if gol > maior:
    maior = gol
    nome_maior = time


print "Time com melhor ataque (%d gol(s)): %s" % (maior, nome_maior)
print "Média de gols marcados: %.1f" % (soma / numero_de_times)
    
24.10.2018 / 21:15
0

Your problem can be solved by using the index method of the list, which will find the index where the value is inside the list. For example, in the lista = [1, 2, 3, 4] list, the result of lista.index(3) would be 2, indicating that number 3 is in position 2 of the list.

So, knowing the highest number of goals, with max(gols) , you only have to check in which position this value is in the list and fetch the teams in the same position:

>>> max_gols = max(gols)
>>> indice_vencedor = gols.index(max_gols)
>>> time_vencedor = times[indice_vencedor]

Then the value of time_vencedor would be the name of the team that had the most goals.

However, it is not interesting to keep two values correlated in different structures. Each team has a goal balance, so you better save the information together. In Python, to store correlated data of different types we use the tuple, getting something like ('time A', 10) or ('time B', 8) . But as this way it is not very clear what values 10 and 8 represent you can use the named tuple which leaves the code a bit more readable ( for version 3.6 + ):

from typing import NamedTuple

class Time(NamedTuple):
    nome: str
    pontuação: int

So you can create the team list:

x = int(input('Quantos times? '))

times = []

for i in range(x):
    nome = input('Nome: ')
    pontuação = int(input('Pontuação: '))
    time = Time(nome, pontuação)
    times.append(time)

And finally check the winner and the average score:

vencedor = max(times, key=lambda time: time.pontuação)
média = sum(time.pontuação for time in times) / len(times)

Displaying on screen:

print(f'O time vencedor foi {vencedor.nome} com uma pontuação de {vencedor.pontuação}')
print(f'A média das pontuações foi {média}')

Code Running | See in the Repl.it

For any team you can access the nome and punctuation 'attributes, you can also order the teams, generating the championship classification:

for time in sorted(times, key=lambda time: time.pontuação, reverse=True):
    print(f'{time.nome}, com {time.pontuação} pontos')

What would generate an output like:

>>> Quantos times?  3
>>> Nome:  Time A
>>> Pontuação:  13
>>> Nome:  Time B
>>> Pontuação:  15
>>> Nome:  Time C
>>> Pontuação:  10
Time B, com 15 pontos
Time A, com 13 pontos
Time C, com 10 pontos

Code Running | See in Repl.it

    
24.10.2018 / 22:03