Python3:sorted () function and the sort () function in the list
- Reprint please indicate author and source: http://blog.csdn.net/u011475210
- Operating system: WINDOWS 10
- Software version: PYTHON-3.6.2-AMD64
- A compilation?? By: wordzzzz
First, sort,sorted function introduction:
?? The sort function is a function in the list, and sorted can sort lists or iterator.
?? Below we use Help to see their usage and features:
Sort
>>> help(list.sort)Help on method_descriptor:sort(...) L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE*
Sorted
python3.x:
>> > Help (sorted)-on Built-in function sorted in module Builtins:sorted (iterable,/, *, Key=none, Reverse=False) Return a new list containing all items from the iterable in Ascendi Ng order. A Custom key function can be supplied to customize the sort order, and the reverse flag can be Set to request the result in descending order.
python2.x:
>>> help(sorted)Help on built-in function sorted in module __builtin__:sorted(...) sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted list
?? Well, the sorted function of python3.x and python2.x is a bit different, and the CMP parameter is missing. Below this slag is mainly based on python2.x sorted function to explain, python3.x directly ignore the parameters of CMP, in order to ensure code commonality, not recommended for future programming in the use of CMP parameters.
Ii. comparison of sort and sorted:
?? Sorting a list with the Sort function affects the list itself, and sorted does not.
Example:
>>> a = [1,2,1,4,3,5]>>> A.sort ()>>> a[1,1, 2, 3, 4, 5]>>> a = [1,2,1,4,3,5]>>> Sorted (a) [1, 1, 2, 3, 4, 5]>>> a[1, 2, 1, 4, 3, 5]
?? Sorted function of python2.x: sorted (iterable,cmp,key,reverse)
Parameters:
-Iterable can be a list or a iterator;
-CMP is a comparison function with two parameters;
-Key is a function with one parameter;
-Reverse is false or true;
To illustrate:
(1) Sort by CMP function:
>>> List1 = [(' David ',90), (' Mary ',90), (' Sara ',80), (' Lily ',95)]>>> sorted (list1,cmp =lambda x,y:cmp (X[0],y[0])) [( Span class= "hljs-string" > ' David ', 90), ( ' Lily ', 95", ( "Mary", 90), ( Sara ', 80)]>>> sorted (list1,cmp = lambda x,y:cmp (X[1],y[1])) [( ' Sara ', 80), ( ' David ', 90", ( "Mary", 90), ( Lily ', 95)]
(2) Sort by key function:
>>> List1 = [(' David ',90), (' Mary ',90), (' Sara ',80), (' Lily ',95)] >>> sorted (List1,key = lambda List1:list1[0]) [( ' David ', 90), ( 95), ( ' Mary ', 90", ( ' Sara ', 80)] >>> sorted (List1,key = lambda list1:list1[1]) [( ' Sara ', 80), ( ' David ', 90), ( ' Mary ', 90), ( Lily ', 95)]
(3) Sort by reverse:
>>> sorted(list1,reverse = True)[(‘sara‘, 80), (‘mary‘, 90), (‘lily‘, 95), (‘david‘, 90)]
(4) Sort by Operator.itemgetter function:
>>>from operator import itemgetter >>> Sorted (List1, Key=itemgetter (1)) [( ' Sara ', 80), ( ' David ', 90), ( 90), ( ' Lily ', 95)]>>> sorted (List1, Key=itemgetter (0 ) [( ' David ', 90), ( ' Lily ', 95), ( ' Mary ', 90), ( ' Sara ', 80)]
?? Describes the Operator.itemgetter function:
>>> import operator>>> a = [1,2,3]>>> b = operator.itemgetter(0)>>> b(a)1
?? The Operator.itemgetter function does not get a value, but instead defines a function.
(5) Multilevel sequencing:
>>> sorted(list1, key=itemgetter(0,1))[(‘david‘, 90), (‘lily‘, 95), (‘mary‘, 90), (‘sara‘, 80)]
Go Python3:sorted () function and the sort () function in the list