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

Asked

Viewed 67 times

0

I’m stuck in an exercise asking for an X number of teams participating in a championship. In the later lines is typed the name of the team and its score. In the output, the team that made more scores 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 created an empty list to save goals and teams. If only to know the highest score I would have finished. But I’m having trouble returning the name of the team and its score. Can anyone help me?

2 answers

5


There is no need to store all teams and their number of goals - just store only one: the largest you’ve seen so far - if, during running, you find a larger number of goals than you’ve seen before, change the content of your variable and start storing the new champion. In the end, the champion will be the team that has left 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)

0

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

  • 2

    Probably in a real program storing all teams would be necessary, but for what was requested is a waste of memory/processing

  • @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.

  • 2

    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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.