Both methods python multi-level comparison

Description

We all know that the time to buy things, the site will be given a lot of merchandise on the electricity supplier website according to our search criteria. These items will be ordered in accordance with a certain kind of collation, turn in front of us. Now a book about electricity supplier website Sort There are so few, integrated sales, publication date, price, user rating. Ranking assume the rule is: First look at the price, low price of the top-ranking, if the price is the same, the time of publishing, publishing rankings of late forward, if both are the same then look at sales, sales of large front if the first three are the same, and finally see user ratings, higher user ratings ranking.

Please write a book based on the rules to each program on a consolidated ranking.

Input

Conduct a first integer n (1 <n <100), followed by n rows book data, a total of 7, separated by spaces between each column. Wherein the first listed title (length of less than 60 and containing only uppercase and lowercase letter string), the second column to the seventh column are equal to an integer greater than 0, representing sales book, published in time , month, date, price and user rating.

 

Output

n lines of output of a book, consistent with the input format (in the test data to ensure that books do not rank the same, and all integers can be used to store int).

Sample Input Copy

3
CPrimerPlus 3000 2013 12 6 60 44
ComputerSystemsAProgrammersPerspective 8000 2015 3 12 156 39
TheCProguammingLanguage 50000 1978 2 22 5 46

Sample Output Copy

TheCProguammingLanguage 50000 1978 2 22 5 46
CPrimerPlus 3000 2013 12 6 60 44
ComputerSystemsAProgrammersPerspective 8000 2015 3 12 156 39


Python fourth Oj have such a problem. The default sort function to a comparison of the first, if nested conditional sort of words are too cumbersome, people look at this code is also feeling a little dizzy. Think of the sort function has a key argument can call the function returns an iterator object, so I think there are ways to achieve multi-level comparison of key parameters. Access to information discovery of two ways.

1.lambda returns a tuple

Because the list sort function is installed in order to sort data; thus will be exchanged with lambda list location of the data sorting can be installed at certain locations of the data.
If you need a list of python multi-level sorting. We have the following data:
 
list_num = [[12,3],[18,34],[18,10],[12,45],[18,10],[8,34]]
 
It needs from small to large order. To compare the first number, if the first number is equal to the second number, then the comparison. code show as below:
 
print(sorted(list_num))
//OUTPUT:[[8, 34], [12, 3], [12, 45], [18, 10], [18, 10], [18, 34]]
# Sorted first with the second number, if the first number is equal to then sorted
print(list_num, key = lambda x:(int(x[0]),int(x[1])))
//output:[[12, 3], [18, 10], [18, 10], [8, 34], [18, 34], [12, 45]]*①
 

The general form of lambda lambda keyword is followed by one or more parameters, followed by a colon, the future is an expression. lambda is an expression rather than a statement. It can appear in places like the Python syntax does not allow def arise. As expression, lambda returns a value (i.e., a new function). lambda used to write a simple function, but def more powerful to handle tasks.

 Example: lambda] 1 using [

  1, the general form

    f = lambda x, y, z :x+y+z

    print f(1,2,3)

    #output:6 *②

 

2.itemgetter

What dimensional data, operator function parameters itemgetter module provides for a number of the object for acquiring the serial number. See the examples below

Example:

from operator import itemgetter

a = [1,2,3] 

b = operator.itemgetter (1,0) // define the function b, obtaining a first object and a field value of 0
>>> B (A) 
(2,. 1)

To note, operator.itemgetter not function gets the value, but the definition of a function, in order to get the value of the object via the function role. * ③

 

Example:

sorted(student_tuples, key=itemgetter(1,2))

itemgetter parameter is a list of dictionary index or index arranged in ordered by priority * ④

 

references:

①https: //blog.csdn.net/w417950004/article/details/86253721 (multi-level sorting)

②https: //www.cnblogs.com/mxh1099/p/5386529.html (lambda expressions)

③https: //www.cnblogs.com/zhoufankui/p/6274172.html (itemgetter function)

④https: //blog.csdn.net/jb19900111/article/details/50649932 (sort, sorted advanced sorting)

Guess you like

Origin www.cnblogs.com/protectione055/p/11723566.html