Your problem can be solved by using the method index
of the list, which will find the index where the value is inside it. For example, in the list lista = [1, 2, 3, 4]
, the result of lista.index(3)
would be 2, indicating that number 3 occupies position 2 of the list.
So knowing the highest number of goals, with max(gols)
, just check the position this value is in the list and search in the teams the value 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 that you keep two values correlated in different structures. Each team has a goal balance, so you better keep 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 represent the values 10 and 8 you can use the named tuple that leaves the code a little more readable (for version 3.6+):
from typing import NamedTuple
class Time(NamedTuple):
nome: str
pontuação: int
So you can create the list of teams:
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 the 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 working | See on Repl.it
For any team you can access the attributes nome
and score`, can also order the teams, generating the league 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 exit 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 working | See on Repl.it
Probably in a real program storing all teams would be necessary, but for what was requested is a waste of memory/processing
– nosklo
@nosklo in Python, a lot is "waste" of memory :D And this way the code becomes more versatile for future changes, such as generating the classification table. Without persisting the separate score of each team would not be possible. It is important to optimize, but first it is necessary to have performance problem.
– Woss
It doesn’t mean you have to waste it on purpose - I’m all for waste if it makes the code easier to understand, but in this case, I think it’s simple. Besides, the waste in this case is too great, it’s orders of magnitude too much. Imagine 10,000 teams in memory vs 1 time; It’s good to learn how to save
– nosklo