[Personal Notes] Python-sorted () function

1.1 sorted () function

  sorted ()  function sort operation on all objects iterations.

grammar

sorted syntax:

sorted(iterable, key=None, reverse=False)

Parameter Description:

  • iterable - iterables.
  • key - is mainly used for the comparison element, only one parameter, the specific parameter is a function of iteration may be taken from the object, specify one of the elements in the iteration to be sorted.
  • reverse - collation, reverse = True descending, reverse = False ascending (default).

return value

Return to re-sort the list.

Note: sorted () in descending or ascending order, no effect for the content is not reversed order.

 

Examples of presentation

# The sorted () Example 
# define a list 
list_num = [. 1, 2,. 3,. 4,. 5,. 6,. 7,. 8 ]
 Print ( ' original list is: ' , list_num)
 # Reverse default is False (ascending), to True was descending 
A = the sorted (list_num, Reverse = True)
 Print ( ' after descending order: ' , A)

Output:

 

 

 

# The sorted () Example 
# define a list 
list_num = [. 1, 2,. 3,. 4,. 5,. 6,. 7,. 8 ]
 Print ( ' original list is: ' , list_num)
 # using the key in descending order of 
a = sorted (list_num, = Key the lambda X: X * -1 )
 Print ( ' after descending order: ' , A)

 

 

Guess you like

Origin www.cnblogs.com/cxstudypython/p/11969725.html