Home > Net >  Sort the nested dictionary based on several conditions
Sort the nested dictionary based on several conditions

Time:11-12

How do I sort a nested dictionary in descending order by points, if there were several teams with the same points, then based on the number of wins and if several teams had the same points and the same number of wins then based on the names of the teams in alphabetical order.

Like the example below :

Input = {
'England': {'wins': 1, 'loses': 1, 'draws': 1, 'goal difference': 0, 'points': 4}, 
'Spain': {'wins': 1, 'loses': 0, 'draws': 2, 'goal difference': 2, 'points': 5}, 
'Belgium': {'wins': 1, 'loses': 1, 'draws': 1, 'goal difference': 0, 'points': 4}, 
'Argentina': {'wins': 1, 'loses': 2, 'draws': 0, 'goal difference': -2, 'points': 3}
}

Output in print :

Spain  wins:1 , loses:0 , draws:2 , goal difference:2 , points:5

Belgium  wins:1 , loses:1 , draws:1 , goal difference:0 , points:4

England  wins:1 , loses:1 , draws:1 , goal difference:0 , points:4

Argentina  wins:1 , loses:2 , draws:0 , goal difference:-2 , points:3

CodePudding user response:

Items in a dictionary do not have an order, and therefore cannot be sorted.

I would recommend storing these items as a list of tuples. Each tuple would have the name of the country as one element, and and the list of numbers as the other element:

A = [("England", [1, 1, 1, 0, 4]), ("Spain", [1, 0, 2, 2, 5])]

Then you can sort the array by implementing a comparator function and passing it to the built-in sorted() function along with your list.

CodePudding user response:

You can't sort a dictionary as its keys are not in order. But you can get a sorted list of keys and values by using the sorted function like bellow :

Input = {'England': {'wins': 1, 'loses': 1, 'draws': 1, 'goal difference': 0, 'points': 4}, 'Spain': {'wins': 1, 'loses': 0, 'draws': 2, 'goal difference': 2, 'points': 5}, 'Belgium': {'wins': 1, 'loses': 1, 'draws': 1, 'goal difference': 0, 'points': 4}, 'Argentina': {'wins': 1, 'loses': 2, 'draws': 0, 'goal difference': -2, 'points': 3}}

sorted_input = sorted(Input.items(), key=lambda x: x[1]['points'], reverse=True)

print(Input)

for country, stats in sorted_input:
    print(country, stats)

It has this syntax : sorted(iterable, key=key, reverse=reverse)

iterable Required. The sequence to sort, list, dictionary, tuple etc.

key Optional. A Function to execute to decide the order. Default is None

reverse Optional. A Boolean. False will sort ascending, True will sort descending. Default is False

It creates a list of pairs of keys and values

CodePudding user response:

You can give a key function to sorted to tell it by which criterion to sort. If the criterion is a tuple, then these tuples will be compared in lexicographical order, meaning the first item of the tuple is compared first, and in case of a tie the second item is compared, etc.

Input = {
'England': {'wins': 1, 'loses': 1, 'draws': 1, 'goal difference': 0, 'points': 4}, 
'Spain': {'wins': 1, 'loses': 0, 'draws': 2, 'goal difference': 2, 'points': 5}, 
'Belgium': {'wins': 1, 'loses': 1, 'draws': 1, 'goal difference': 0, 'points': 4}, 
'Argentina': {'wins': 1, 'loses': 2, 'draws': 0, 'goal difference': -2, 'points': 3}
}

for country,stats in sorted(Input.items(), key=lambda x:(-x[1]['points'],-x[1]['wins'],x[0])):
    print('{:12}'.format(country), end='')
    for name,number in stats.items():
        print('{}:{:2}'.format(name, number), end=', ')
    print()

# Spain       wins: 1, loses: 0, draws: 2, goal difference: 2, points: 5, 
# Belgium     wins: 1, loses: 1, draws: 1, goal difference: 0, points: 4, 
# England     wins: 1, loses: 1, draws: 1, goal difference: 0, points: 4, 
# Argentina   wins: 1, loses: 2, draws: 0, goal difference:-2, points: 3,
  • Related