I am trying to sort this dictionary that contains divisions of hockey teams. Each team is an element of the dictionary and each team's value is a dictionary that has a number of points (PTS) and a VRP stat.
I'm trying to sort the teams inside each division according to their points in descending order. The sorting is independent in each division.
Here is my code:
dic = {
"Atlantic": {
"TeamA": {"PTS": 80, "VRP": 35},
"TeamB": {"PTS": 65, "VRP": 31},
"TeamC": {"PTS": 80, "VRP": 30}
},
"Metropolitan": {
"TeamD": {"PTS": 60, "VRP": 29},
"TeamE": {"PTS": 65, "VRP": 28}
}
}
def ranking_sorting(ranking):
for division in ranking.values():
division = dict(sorted(division.items(), key=lambda x:x[1]["PTS"], reverse = True))
ranking_sorting(dic)
print(dic)
I tried to use a lambda function as a key in the sorted function to access the PTS value inside the team's value with key=lambda x:[1]["PTS"].
Here is the result I expected:
{
"Atlantic": {
"TeamA": {"PTS": 80, "VRP": 35},
"TeamC": {"PTS": 80, "VRP": 30}
"TeamB": {"PTS": 65, "VRP": 31},
},
"Metropolitan": {
"TeamE": {"PTS": 65, "VRP": 28}
"TeamD": {"PTS": 60, "VRP": 29},
}
}
When I try to print the dictionary I get back the original one but it's not sorted.
I figured it out. I need to assign the "divsion" teams to the division:
The division teams did get sorted, but I didn't assign them to the main dictionary.