Implementation of Multi-condition sorting in Python and Analysis of python conditional sorting
Multi-condition sorting and itemgetter Application
My colleagues on the client used as to write a lot of code to sort Python. When I learned that python sorting usually only requires one line, I was very surprised and became very interested in Python.
In the past, we used multi-condition sorting to make the football point list. If the points are the same, we will win the game by the same number of points, then we will also press the number of players in the same way.
That is, in the order of Points P, goal GD, goal GS, and loss GA.
In python, sorting is very convenient. The sorting parameters mainly include key and reverse. The cmp parameter is not recommended. It is removed in python3.0 and replaced with the key parameter.
Multi-condition sorting is also very simple, just remember the following sentence. That is, the function specified by the parameter key returns a tuples. The order of Multi-condition sorting is in the order of tuples.
After reading the following code, you will understand that the following table lists the points in Group A of the 2010 World Cup group competition.
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)
Output
[{'P': 7, 'GD': 4, 'GS': 4, 'GA': 0, 'team': 'Uruguay'}, {'P': 4, 'GD': 1, 'GS': 3, 'GA': 2, 'team': 'Mexico'}, {'P': 4, 'GD': -2, 'GS': 3, 'GA': 5, 'team': 'SouthAfrica'}, {'P': 1, 'GD': -3, 'GS': 1, 'GA': 4, 'team': 'France'}]
That is, the Group ranks Uruguay, Mexico, South Africa, and France.
However, the key values used for dictionary retrieval are a little too long. itemgetter is more concise and elegant. The above code can be replaced with the following code.
from operator import itemgetter print sorted(teamitems ,key = itemgetter('P','GD','GS','GA'),reverse=True)
Some sort by multiple conditions in ascending order or in descending order.
Previously, when collecting statistics and exporting the consumption of server players in each region, you must sort them in ascending order and in descending order.
This is the requirement. The partition is in the descending order. If the partition server is the same, the consumption is in the descending order.
The implementation method is to use the python sort algorithm to sort data in a stable manner. The data is sorted multiple times, with secondary conditions first and primary conditions in the back.
There is also a more concise method for one stream, but it is only effective when the data to be sorted is a numerical value. This method adds a negative number in front of the opposite number.
The code below.
# Assume that the data is as follows. Data = ''' server, player id, cumulative consumption 3, a, 2380 1, B, 11900 4, e, 3250 1, k, 100 4, j, 599 2, m, 872 3, f, 5560 1, y, 2500 ''' items = [x. split (',') for x in filter (None, data. split ('\ n') [1:] # Remove the empty line and ignore the first line and convert the string into a two-dimensional array # method 1 items. sort (key = lambda x: int (x [2]), reverse = True) # consume items first. sort (key = lambda x: int (x [0]) # Then the partition server prints '\ n '. join ([','. join (x) for x in items]) print '-----------' # method 2 items = sorted (items, key = lambda x :( int (x [0]), -int (x [2]) print '\ n '. join ([','. join (x) for x in items])