How to print a list value one below another

Question:

I need to sort a ranking of points by descending order. The users and points are inside lista_ranking which includes de following code:

[{‘partido’: {‘codigo’: ‘AAA’, ‘fecha’: datetime.date(2022, 11, 20), ‘hora’: ’13:00hs’, ‘equipo_local’: ‘Catar’, ‘equipo_visitante’: ‘Ecuador’, ‘estado’: ‘Finalizado’, ‘goles_local’: 0, ‘goles_visitante’: 1}, ‘usuario’: {‘cedula’: ‘123’, ‘nombre’: ‘Gon’, ‘apellido’: ‘Henderson’, ‘fecha’: ‘(2003, 3, 12)’, ‘puntaje’: 5}, ‘goles_local’: 1, ‘goles_visitante’: 0}, {‘partido’: {‘codigo’: ‘AAA’, ‘fecha’: datetime.date(2022, 11, 20), ‘hora’: ’13:00hs’, ‘equipo_local’: ‘Catar’, ‘equipo_visitante’: ‘Ecuador’, ‘estado’: ‘Finalizado’, ‘goles_local’: 0, ‘goles_visitante’: 1}, ‘usuario’: {‘cedula’: ‘1234’, ‘nombre’: ‘George’, ‘apellido’: ‘Stev’, ‘fecha’: ‘(2003, 3, 12)’, ‘puntaje’: 8}, ‘goles_local’: 0, ‘goles_visitante’: 1}]

With the code

ranking_high_to_low=sorted([(numeros['usuario']['puntaje'], numeros['usuario']['nombre'], numeros['usuario']['apellido']) for numeros in lista_ranking], reverse=True) print(ranking_high_to_low)

It prints the ranking from highest to lowest like this:

[(8, 'George', 'Stev'), (5, 'Gon', 'Henderson')]

Which for should I use in order for it to print the ranking as follows:

George Stev 8
Gon Henderson 5
Asked By: Pani Hardoy

||

Answers:

I think that this should do the trick:

   for (num, first, last) in ranking_high_to_low:
       print("{} {} {}".format(first, last, num))
Answered By: aaryanm23

You are close to the solution. You can use a loop and join the part of the name to print the rank of the users.

import datetime

lista_ranking = [
    {'partido': {'codigo': 'AAA', 'fecha': datetime.date(2022, 11, 20),
                 'hora': '13:00hs', 'equipo_local': 'Catar',
                 'equipo_visitante': 'Ecuador', 'estado': 'Finalizado',
                 'goles_local': 0, 'goles_visitante': 1},
     'usuario': {'cedula': '123', 'nombre': 'Gon',
                 'apellido': 'Henderson',
                 'fecha': '(2003, 3, 12)', 'puntaje': 5},
     'goles_local': 1,
     'goles_visitante': 0}, {
        'partido': {'codigo': 'AAA',
                    'fecha': datetime.date(2022, 11, 20),
                    'hora': '13:00hs', 'equipo_local': 'Catar',
                    'equipo_visitante': 'Ecuador',
                    'estado': 'Finalizado',
                    'goles_local': 0, 'goles_visitante': 1},
        'usuario': {'cedula': '1234', 'nombre': 'George',
                    'apellido': 'Stev', 'fecha': '(2003, 3, 12)',
                    'puntaje': 8}, 'goles_local': 0,
        'goles_visitante': 1}]
ranking_high_to_low = sorted([(numeros['usuario']['puntaje'],
                               numeros['usuario']['nombre'],
                               numeros['usuario']['apellido']) for numeros in
                              lista_ranking], reverse=True)
for info in ranking_high_to_low:
    print(f"{' '.join(info[1:])} {info[0]}")

Output:

George Stev 8
Gon Henderson 5

The benefit of the join method here is how it can print the names with multiple parts (first name, middle name, last name).

Update:

If you want to store unique players and sort them by their total scores, you need to use a dictionary. Then sort the dictionary in reverse order by the scores.

import datetime

lista_ranking = [
    {'partido': {'codigo': 'AAA', 'fecha': datetime.date(2022, 11, 20),
                 'hora': '13:00hs', 'equipo_local': 'Catar',
                 'equipo_visitante': 'Ecuador', 'estado': 'Finalizado',
                 'goles_local': 0, 'goles_visitante': 1},
     'usuario': {'cedula': '123', 'nombre': 'Gon',
                 'apellido': 'Henderson',
                 'fecha': '(2003, 3, 12)', 'puntaje': 5},
     'goles_local': 1,
     'goles_visitante': 0},
    {'partido': {'codigo': 'AAA', 'fecha': datetime.date(2022, 11, 20),
                 'hora': '13:00hs', 'equipo_local': 'Catar',
                 'equipo_visitante': 'Ecuador', 'estado': 'Finalizado',
                 'goles_local': 0, 'goles_visitante': 1},
     'usuario': {'cedula': '123', 'nombre': 'Gon',
                 'apellido': 'Henderson',
                 'fecha': '(2003, 3, 12)', 'puntaje': 5},
     'goles_local': 1,
     'goles_visitante': 0}
    , {
        'partido': {'codigo': 'AAA',
                    'fecha': datetime.date(2022, 11, 20),
                    'hora': '13:00hs', 'equipo_local': 'Catar',
                    'equipo_visitante': 'Ecuador',
                    'estado': 'Finalizado',
                    'goles_local': 0, 'goles_visitante': 1},
        'usuario': {'cedula': '1234', 'nombre': 'George',
                    'apellido': 'Stev', 'fecha': '(2003, 3, 12)',
                    'puntaje': 8}, 'goles_local': 0,
        'goles_visitante': 1}]
ranking_high_to_low = [(numeros['usuario']['puntaje'],
                        numeros['usuario']['nombre'],
                        numeros['usuario']['apellido']) for numeros in
                       lista_ranking]

players = {}
for info in ranking_high_to_low:
    player_name = ' '.join(info[1:])
    players[player_name] = players.get(player_name, 0) + info[0]

for player, score in sorted(players.items(), key=lambda x: x[1], reverse=True):
    print(f"{player} {score}")

Output:

Gon Henderson 10
George Stev 8

References:

Answered By: arsho
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.