1.5list中sort && sorted

sort  && sorted

一、sort 和 sorted排序原理

sort:原位排列列表,无返回(本地排序,不返回副本),例子:

>>> original_list = [1,5,4,3]
>>> original_list.sort()
>>> original_list
[1, 3, 4, 5]

sorted:排列列表,返回新的列表(返回副本,原始输入不变),例子:

>>> original_list = [1,5,4,3]
>>> new_list = sorted(original_list)
>>> new_list
[1, 3, 4, 5]
>>> original_list
[1, 5, 4, 3]

 

二、sort 和 sorted参数、返回

---------------------------sort---------------------------------

>>> help(list.sort)
Help on method_descriptor:

sort(...)
L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*;
cmp(x, y) -> -1, 0, 1

---------------------------sort---------------------------------

---------------------------sorted-------------------------------

>>> help(sorted)
Help on built-in function sorted in module __builtin__:

sorted(...)
sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted list

---------------------------sorted---------------------------------

参数:

iterable:可迭代的类型对象,iteralbe指的是能够一次返回它的一个成员的对象。iterable主要包括3类:

第一类是所有的序列类型,比如list(列表)、str(字符串)、tuple(元组)。   第二类是一些非序列类型,比如dict(字典)、file(文件)。 第三类是你定义的任何包含__iter__()或__getitem__()方法的类的对象。 ---------------------------------第一类例子------------------------------------ 第一类iterable为list例子: >>> original_list = [1,5,4,3]
>>> new_list = sorted(original_list)
>>> new_list
[1, 3, 4, 5]

第一类iterable为str例子:

>>> new_list = sorted("python")
>>> new_list
['h', 'n', 'o', 'p', 't', 'y']

第一类iterable为tuple例子:

>>> sort_tuple = (5,1,2,4)
>>> new_list = sorted(sort_tuple)
>>> new_list
[1, 2, 4, 5]

---------------------------------第一类例子------------------------------------

 

---------------------------------第二类例子------------------------------------

第二类对字典进行排序,实际是对key进行排序,例子:

>>> sort_dict = {"a": 1,"c" : 3,"b" : 2}
>>> new_list = sorted(sort_dict)
>>> new_list
['a', 'b', 'c']

---------------------------------第二类例子------------------------------------

 

 

   

cmp:用于比较的函数,比较神秘由key决定,迭代集合中的每一项

key:用列表元素(或其他客迭代对象)的某个属性和函数作为关键字,有默认值,迭代集合中的每一项

reserve:排序规则,有默认值

返回:一个经过排序的可迭代的类型,与iteable一样

注意:一般来说,cmp和key可以使用lambda表达式。

参数说明: (1)  cmp参数 cmp接受一个函数,拿整形举例,形式为: def f(a,b):      return a-b 如果排序的元素是其他类型的,如果a逻辑小于b,函数返回负数;a逻辑等于b,函数返回0;a逻辑大于b,函数返回正数就行了   (2)  key参数  key也是接受一个函数,不同的是,这个函数只接受一个元素,形式如下 def f(a):      return len(a) key接受的函数返回值,表示此元素的权值,sort将按照权值大小进行排序 key && 模块 operator.itemgetter:operator.itemgetter函数获取的不是值,而是定义了一个函数 例一:

>>> test_list = [1,2,3,4,5,6]
>>> test_fun = itemgetter(2)

>>> test_fun
<operator.itemgetter object at 0x028C4F30>
>>> test_fun(test_list)
3

例二:

>>> test_list = [1,3,6,4,9]
>>> from operator import itemgetter
>>> test_fun = itemgetter(2,3)
>>> result = test_fun(test_list)
>>> result
(6, 4)

 operator.itemgetter函数多级排序

>>> from operator import itemgetter
>>> sort_list = [("david",90),("lily",95),("mary",91),("sara",80)】

>>> new_list = sorted(sort_list,key = itemgetter(0,1))

>>> new_list
[('david', 90), ('lily', 95), ('mary', 91), ('sara', 80)]

 

operator.attrgetter

>>> from operator import attrgetter
>>> class Student:
def __init__(self,name,grade,age):
self.name = name
self.grade = grade
self.age = age


>>> student_object = [Student("lulu",3,22),Student("yangyang",4,23),Student("chunchun",1,20)]
>>> new_list = sorted(student_object,key = attrgetter("grade","age"))
>>> new_list
[<__main__.Student instance at 0x028FD350>, <__main__.Student instance at 0x028FD490>, <__main__.Student instance at 0x028FD4B8>]

  (3) reverse参数 接受False 或者True 表示是否逆序   ---------------------------------cmp例子------------------------------------

>>> sort_list = [{1:5,3:4},{1:3,6:3},{1:1,2:4,5:6},{1:9}]
>>> def f2(a,b):
return a[1] - b[1]

>>> new_list = sorted(sort_list,cmp = f2)
>>> new_list
[{1: 1, 2: 4, 5: 6}, {1: 3, 6: 3}, {1: 5, 3: 4}, {1: 9}]

---------------------------------cmp例子------------------------------------

 

---------------------------------cmp例子------------------------------------

>>> sort_list = [{1:5,3:4},{1:3,6:3},{1:1,2:4,5:6},{1:9}]
>>> def f2(a,b):
return a[1] - b[1]

>>> new_list = sorted(sort_list,cmp = f2)
>>> new_list
[{1: 1, 2: 4, 5: 6}, {1: 3, 6: 3}, {1: 5, 3: 4}, {1: 9}]

---------------------------------cmp例子------------------------------------

  ---------------------------------key例子------------------------------------

>>> sort_list = [{1:5,3:4},{1:3,6:3},{1:1,2:4,5:6},{1:9}]
>>> def f2(a):
return len(a)

>>> new_list = sorted(sort_list,key = f2)
>>> new_list
[{1: 9}, {1: 5, 3: 4}, {1: 3, 6: 3}, {1: 1, 2: 4, 5: 6}]

---------------------------------key例子------------------------------------

 

---------------------------------key和reverse例子------------------------------------

>>> sort_list = [{1:5,3:4},{1:3,6:3},{1:1,2:4,5:6},{1:9}]
>>> def f2(a):
return len(a)

>>> new_list = sorted(sort_list,key = f2)
>>> new_list
[{1: 9}, {1: 5, 3: 4}, {1: 3, 6: 3}, {1: 1, 2: 4, 5: 6}]

---------------------------------key和reverse例子------------------------------------

>>> sort_list = [('2', '3', '10'), ('1', '2', '3'), ('5', '6', '7'), ('2', '5', '10'), ('2', '4', '10')]

>>> new_list = sorted(sort_list,key = lambda x:(int(x[2]),int(x[1])),reverse = False)
>>> new_list
[('1', '2', '3'), ('5', '6', '7'), ('2', '3', '10'), ('2', '4', '10'), ('2', '5', '10')]

---------------------------------key和reverse例子------------------------------------

 

---------------------------------key和operator.itemgetter函数排序例子---------------------------------

 operator.itemgetter函数一级排序

>>> from operator import itemgetter
>>> sort_list = [("david",90),("lily",95),("mary",91),("sara",80)】

>>> new_list = sorted(sort_list,key = itemgetter(0,1))

>>> new_list
[('david', 90), ('lily', 95), ('mary', 91), ('sara', 80)]

>>> from operator import itemgetter
>>> sort_list = [("david",90),("lily",95),("mary",91),("sara",80)]
>>> new_list = sorted(sort_list,key = itemgetter(1))
>>> new_list
[('sara', 80), ('david', 90), ('mary', 91), ('lily', 95)]
>>> new_list2 = sorted(sort_list,key = itemgetter(0))
>>> new_list2
[('david', 90), ('lily', 95), ('mary', 91), ('sara', 80)]

 

 operator.itemgetter函数多级排序

>>> from operator import itemgetter
>>> sort_list = [("david",90),("lily",95),("mary",91),("sara",80)】

>>> new_list = sorted(sort_list,key = itemgetter(0,1))

>>> new_list
[('david', 90), ('lily', 95), ('mary', 91), ('sara', 80)]

---------------------------------key和operator.itemgetter函数排序例子---------------------------------

 operator.attrgetter函数多级排序

 

---------------------------------key和operator.attrgetter函数排序例子---------------------------------

>>> from operator import attrgetter
>>> class Student:
def __init__(self,name,grade,age):
self.name = name
self.grade = grade
self.age = age


>>> student_object = [Student("lulu",3,22),Student("yangyang",4,23),Student("chunchun",1,20)]
>>> new_list = sorted(student_object,key = attrgetter("grade","age"))
>>> new_list
[<__main__.Student instance at 0x028FD350>, <__main__.Student instance at 0x028FD490>, <__main__.Student instance at 0x028FD4B8>]

 

---------------------------------key和operator.attrgetter函数排序例子---------------------------------

 

 

你可能感兴趣的