In the process of writing a project, you can always find the sort of dictionary, and if you want to implement a multi-conditional sort, you only need the following lines of code to implement. Fully embodies the benefits of Python.
Copy the Code code as follows:
Teamitems = [{' Team ': ' France ', ' P ': 1, ' GD ': -3, ' GS ': 1, ' GA ': 4},
{' Team ': ' Uruguay ', ' P ': 7, ' GD ': 4, ' GS ': 4, ' GA ': 0},
{' Team ': ' SouthAfrica ', ' P ': 4, ' GD ': -2, ' GS ': 3, ' GA ': 5},
{' Team ': ' Mexico ', ' P ': 4, ' GD ': 1, ' GS ': 3, ' GA ': 2}]
Print sorted (teamitems, key = lambda x: (x[' P '],x[' GD '],x[' GS '],x[' GA '), reverse=true)
The above code implemented by pressing ' P ', ' GD ', ' GS ', ' GA ' four conditional sorting, reverse=true for descending
Of course I can.
Copy the Code code as follows:
From operator Import Itemgetter
Print sorted (teamitems, key = Itemgetter (' P ', ' GD ', ' GS ', ' GA '), reverse=true)