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
>Solution :
I think that this should do the trick:
for (num, first, last) in ranking_high_to_low:
print("{} {} {}".format(first, last, num))