sort and sorted 区别

sort是容器的函数:sort(cmp=None, key=None, reverse=False)

对于一个无序的列表list,调用list.sort(),对list进行排序后返回list,sort()函数修改待排序的列表内容。

对原列表进行排序

通过查询,新版本中 sort参数只保留了两个参数key,reverse 去掉了老版本中cmp参数

cmp -- 可选参数, 如果指定了该参数会使用该参数的方法进行排序。

新版本:python3.6.4

    def sort(self, key=None, reverse=False): # real signature unknown; restored from __doc__
        """ L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE* """
        pass

list.sort(key=None, reverse=False)

参数

  • key:用列表元素的某个属性或函数作为关键字。
  • reverse:排序规则,可以选择True或者False。

返回值

该方法没有返回值,但是会对列表的对象进行排序。

 1 list=[4,5,1,98,12,3]
 2 list.sort()
 3 print(list)
 4 
 5 list2=[4,5,1,98,12,3]
 6 list2.sort(reverse=True)
 7 print(list2)
 8 
 9 
10 
11 # 获取列表的第二个元素
12 def takeSecond(elem):
13     return elem[1]
14 # 列表
15 random = [(2, 2), (3, 4), (4, 1), (1, 3)]
16 # 指定第二个元素排序
17 random.sort(key=takeSecond)
18 # 输出类别
19 print('排序列表:', random)
20 
21 random.sort(key=takeSecond,reverse=True)
22 # 输出类别
23 print('排序列表:', random)
24 
25 random.sort()
26 # 输出类别
27 print('排序列表:', random)
28 
29 random.sort(reverse=True)
30 # 输出类别
31 print('排序列表:', random)

结果:

扫描二维码关注公众号,回复: 1469155 查看本文章
"D:\Program Files (x86)\python36\python.exe" F:/python从入门到放弃/6.5/list复习.py
[1, 3, 4, 5, 12, 98]
[98, 12, 5, 4, 3, 1]
排序列表: [(4, 1), (2, 2), (1, 3), (3, 4)]
排序列表: [(3, 4), (1, 3), (2, 2), (4, 1)]
排序列表: [(1, 3), (2, 2), (3, 4), (4, 1)]
排序列表: [(4, 1), (3, 4), (2, 2), (1, 3)]

Process finished with exit code 0

sorted是python的内建函数:sorted(iterable, cmp=None, key=None, reverse=False)

而对于同样一个无序的列表list,调用sorted(list),对list进行排序后返回一个新的列表list_new,而对list不产生影响。

def sorted(*args, **kwargs): # real signature unknown
    """
    Return a new list containing all items from the iterable in ascending 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.
    """
    pass

举例:

 1 # -*- coding: utf-8 -*-
 2 list=[4,5,1,98,-12,3]
 3 
 4 new_list=sorted(list)
 5 print(new_list)
 6 new_list=sorted(list,reverse=True)
 7 print(new_list)
 8 
 9 #key指定的函数将作用于list的每一个元素上,并根据key函数返回的结果进行排序。
10 new_list2=sorted(list,key=abs)
11 print(new_list2)
12 
13 #下一列表是学生姓名和成绩
14 list3 = [('Bob', 75), ('Adam', 92), ('Bart', 66), ('Lisa', 88)]
15 #按名字排序:
16 def by_nam(name):
17     return name[0]
18 new_list3=sorted(list3,key=by_nam)
19 print(new_list3)
20 new_list3=sorted(list3,key=by_nam,reverse=True)
21 print(new_list3)
22 
23 #按成绩排序:
24 def by_core(name):
25     return name[1]
26 new_list4=sorted(list3,key=by_core)
27 print(new_list4)
28 new_list4=sorted(list3,key=by_core,reverse=True)
29 print(new_list4)

结果:

"D:\Program Files (x86)\python36\python.exe" F:/python从入门到放弃/6.5/list复习.py
[-12, 1, 3, 4, 5, 98]
[98, 5, 4, 3, 1, -12]
[1, 3, 4, 5, -12, 98]
[('Adam', 92), ('Bart', 66), ('Bob', 75), ('Lisa', 88)]
[('Lisa', 88), ('Bob', 75), ('Bart', 66), ('Adam', 92)]
[('Bart', 66), ('Bob', 75), ('Lisa', 88), ('Adam', 92)]
[('Adam', 92), ('Lisa', 88), ('Bob', 75), ('Bart', 66)]

Process finished with exit code 0

猜你喜欢

转载自www.cnblogs.com/Mengchangxin/p/9138911.html