python3 sort sorted (key = lambda) - implemented dictionary sorted key values

Use python list (list) sort, that simple and simple, complex and complicated to say, when I started to learn also do not know what to say, can only look at the search for some articles in English to explain, now we have accumulated some experience, write here to share with you, we have to explain in detail the specific usage functions sorted by example:

  1. First create a list a                                                                                                                                                       

  2.               

  3. Directly sorted method returns a list that is sorted                                                                                                             

  4.                                                                                                                                              

  5. If a list is made of tuples, which when in trouble, we need to use the parameter key, that is, keywords, look at the following sentence command, lambda is an implicit function is fixed wording, instead of writing another word ; x is an element of the list, here, it represents a tuple, x just played a temporary name, you can use any name; x [0] represents a tuple of the first element, a second course element is the x [1]; it is meant the command sentence is sorted by a first element in the list. A is a dict, with                                                         

  6.             

  7. According to a second sorting elements:                                                                                               

  8.      

  9. We can also use the reverse argument achieve reverse order                                                                                                   

  10.      

  11.  cmp python3 cancel parameters can be used functools.cmp_to_key  instead of the reference: https://www.zhihu.com/question/30389643?sort=created

 
 
 
 

Use python list (list) sort, that simple and simple, complex and complicated to say, when I started to learn also do not know what to say, can only look at the search for some articles in English to explain, now we have accumulated some experience, write here to share with you, we have to explain in detail the specific usage functions sorted by example:

  1. First create a list a                                                                                                                                                       

  2.               

  3. Directly sorted method returns a list that is sorted                                                                                                             

  4.                                                                                                                                              

  5. If a list is made of tuples, which when in trouble, we need to use the parameter key, that is, keywords, look at the following sentence command, lambda is an implicit function is fixed wording, instead of writing another word ; x is an element of the list, here, it represents a tuple, x just played a temporary name, you can use any name; x [0] represents a tuple of the first element, a second course element is the x [1]; it is meant the command sentence is sorted by a first element in the list. A is a dict, with                                                         

  6.             

  7. According to a second sorting elements:                                                                                               

  8.      

  9. We can also use the reverse argument achieve reverse order                                                                                                   

  10.      

  11.  cmp python3 cancel parameters can be used functools.cmp_to_key  instead of the reference: https://www.zhihu.com/question/30389643?sort=created

Guess you like

Origin www.cnblogs.com/xyao1/p/11022127.html