Use of the Sort method (or sorted built-in function) of the list in the "Go" python

Source: Internet
Author: User
Tags python list

Original source: http://gaopenghigh.iteye.com/blog/1483864

Python list sorting

Simply take a look at the use of the sort method of list in Python (or the sorted built-in function).

Key words:
Python list sort python dictionary sort sorted


The elements of a list can be a variety of things, strings, dictionaries, classes of their own definition, and so on.

The sorted function uses the following:

Python code
    1. Sorted (data, cmp=none, key=None, reverse=False)


Where data is to be sorted, you can make list or iterator, CMP, and key are functions, and the two functions work with the elements of data to produce a result, and the sorted method is sorted according to this result.
CMP (E1, E2) is a comparison function with two parameters, the return value: Negative Number: E1 < e2, 0:e1 = = e2, positive: E1 > E2. The default is None, which is the built-in comparison function.
Key is a function with one parameter, which is used to extract the comparison value for each element. The default is None, which is to directly compare each element.
In general, key and reverse are much faster than CMP because they are processed only once for each element; And CMP handles it many times.

Use examples to illustrate the usage of sorted:

1. Sorting a list of tuple components

Python code
    1. >>> students = [('John ', ' A ', ' ), ('Jane ', ' B ', ' ), (' Dave ', ' B ', 10),]



Sort using the key function (for lambda use see note 1)

Python code
    1. >>> sorted (students, key=Lambda student:student[2]) # sort by age
    2. [('Dave ', ' B ', ten), (' Jane ', ' B ', ') , (' John ', ' A ', ')]



Sort by CMP function

Python code
    1. >>> sorted (students, cmp=Lambda x,y:cmp (x[2], y[2]) # sort by age
    2. [('Dave ', ' B ', ten), (' Jane ', ' B ', ') , (' John ', ' A ', ')]



Use the operator function to speed up the order, which is equivalent to: (see note 2 for itemgetter usage)

Python code
    1. >>> from operator import itemgetter, Attrgetter
    2. >>> sorted (students, Key=itemgetter (2))



Multilevel sorting with operator function

Python code
    1. >>> sorted (students, Key=itemgetter (1,2)) # Sort by grade
    2. [('John ', ' A ', '), (' Dave ', ' B ', ten), (' Jane ', ' B ', ' )]




2. Sort by dictionary

Python code
    1. >>> d = { ' data1 ': 3,  ' data2 ': 1, 2,  ' data4 ': 4}&NBSP;&NBSP;
    2. >>> sorted (D.iteritems (),  key= Itemgetter (1),  reverse=true)   
    3. [( ' data4 ',  4),  ( ' data1 ',  < Span class= "number" >3),  ( ' data3 ',  2),  ( ' data2 ',  1)]  



Note 1
Reference: http://jasonwu.me/2011/10/29/introduce-to-python-lambda.html

NOTE 2
Reference: http://ar.newsmth.net/thread-90745710c90cf1.html
Class Itemgetter (__builtin__.object)
| Itemgetter (item, ...)-Itemgetter object
|
| Return a callable object that fetches the given item (s) from its operand.
| After, F=itemgetter (2), the call F (r) returns R[2].
| After, G=itemgetter (2,5,3), the call G (r) returns (R[2], r[5], r[3])

Equivalent

Python code
  1. def itemgetter (i,*a):
  2. def func (obj):
  3. r = Obj[i]
  4. If a:
  5. R = (r,) + tuple (Obj[i] for i in a)
  6. return R
  7. return func
  8. >>> a = [1,2,3]
  9. >>> B=operator.itemgetter (1)
  10. >>> B (a)
  11. 2
  12. >>> B=operator.itemgetter (1,0)
  13. >>> B (a)
  14. (2, 1)
  15. >>> B=itemgetter (1)
  16. >>> B (a)
  17. 2
  18. >>> B=itemgetter (1,0)
  19. >>> B (a)
  20. (2, 1)



Resources:
1. http://www.linuxso.com/linuxbiancheng/13340.html
2. http://www.douban.com/note/13460891/

Use of the Sort method (or sorted built-in function) of the list in the "Go" python

Related Article

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.