Sorting in python-multi-attribute sorting

Source: Internet
Author: User

Abstract: Sorting is a classic problem. In python, sorting is easier because of library functions. This article mainly explains how to use python for sorting. For example, how to sort the list in the forward order of key1 and the reverse order of key2; if the list is in the array length, if it is in the last letter sequence of the element.


1. sort function description:

The sort function is a method of the list class, which is described as follows:

Sort (...)
L. sort (cmp = None, key = None, reverse = False) -- stable sort * in place *;
Cmp (x, y)->-1, 0, 1

The key and reverse parameters include cmp, key, and reverse. cmp is used to specify the size comparison algorithm for sorting. key is used to determine the dimension and priority of sorting; reverse indicates whether it is in reverse order (True indicates from large to small)


2. Instance

2.1 sort the tuples in ascending order based on the first dimension, and the second dimension is also ascending. (Or both are in reverse order)

Students. sort (key = lambda l :( l [0], l [1]), reverse = True)
Students. sort (key = lambda l :( l [0], l [1])

2.2 sort a group of data in ascending order: def mycmp (a, B ):
If a [0]> B [0]:
Return 1
If a [0]
Return-1
If a [1]
Return 1
If a [1]> B [1]:
Return-1
Return 0
If _ name __= = "_ main __":
# Students. sort (key = lambda l :( l [0], l [1]), reverse = True)
Students. sort (cmp = mycmp)


3. python built-in function sort

A = [5, 2, 1, 9, 6]
>>> Sorted (a) # Sort a from small to large without affecting the structure of
[1, 2, 5, 6, 9]
>>> Sorted (a, reverse = True) # Sort a from large to small without affecting the structure of.
[9, 6, 5, 2, 1]
>>> A. sort () # sort a from small to large, affecting the structure of
>>>
[1, 2, 5, 6, 9]
>>> A. sort (reverse = True) # sort a from large to small, affecting the structure of.
>>>
[9, 6, 5, 2, 1]
Note that a. sort () has changed its structure, and B = a. sort () is an incorrect syntax!
>>> B = ['A', 'bb ', 'bb', 'zz ', 'cc']
>>> Sorted (B)
['Bb ', 'cc', 'AA', 'bb', 'zz '] # sort by the ascii code of each letter in the list element from small to large, use sorted (B, reverse = True)
>>> C = ['ccc ', 'bb', 'ffffff', 'z']
>>> Sorted (c, key = len) # sort by the length of the list element
['Z', 'bb ', 'ccc', 'ffffff']
>>> D = ['ccc ', 'bb', 'ffffff', 'z']
>>> Sorted (d, key = str. lower) # converts each element in the list to lowercase, and then sorts it by the ascii code of each letter in each element from small to large.
['Bb ', 'ccc', 'ffffff', 'z']
>>> Def lastchar (s ):
Return s [-1]
>>> E = ['abc', 'B', 'aaz', 'ef ']
>>> Sorted (e, key = lastchar) # Sort a custom function. lastchar is the function name. This function returns the last letter of each element in List e.
['B', 'abc', 'ef ', 'aaz'] # sorted (e, key = lastchar) the function is to sort by the ascii code of the last letter of each element in List e from small to large.
>>> F = [{'name': 'abc', 'age': 20}, {'name': 'def', 'age': 30 }, {'name': 'ghi', 'age': 25}] # the elements in the list are dictionaries.
>>> Def age (s ):
Return s ['age']
>>> Ff = sorted (f, key = age) # Sort the custom function by the age in the dictionary in list f from small to large.
[{'Age': 20, 'name': 'abc'}, {'age': 25, 'name': 'ghi'}, {'age': 30, 'name': 'def '}]
>>> F2 = sorted (f, key = lambda x: x ['age']) # If you think the Code defined above is not beautiful, you can define a function in the form of lambda, with the same effect as above.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.