ToB企服应用市场:ToB评测及商务社交产业平台

标题: Python中用来排序的方法sort、sorted [打印本页]

作者: 守听    时间: 2024-7-20 13:54
标题: Python中用来排序的方法sort、sorted
sort 与 sorted 区别:

sort方法原型:
  1. >>> help(list.sort)
  2. L.sort(cmp=None, key=None, reverse=False)
复制代码
sorted方法原型:
  1. >>> help(sorted)
  2. sorted(iterable,key=None, reverse=False)
复制代码
参数说明:
sorted使用方法

1、对列表排序,返回的对象不会改变原列表,返回新列表
  1. list = [1,5,7,2,4]
  2. sorted(list)
  3. Out[87]: [1, 2, 4, 5, 7]
  4. #可以设定时候排序方式,默认从小到大,设定reverse = False 可以从大到小
  5. sorted(list,reverse=False)
  6. Out[88]: [1, 2, 4, 5, 7]
  7. sorted(list,reverse=True)
  8. Out[89]: [7, 5, 4, 2, 1]
复制代码
2、根据自界说规则来排序,使用参数:key
  1. # 使用key,默认搭配lambda函数使用
  2. sorted(chars,key=lambda x:len(x))
  3. Out[92]: ['a', 'is', 'boy', 'bruce', 'handsome']
  4. sorted(chars,key=lambda x:len(x),reverse= True)
  5. Out[93]: ['handsome', 'bruce', 'boy', 'is', 'a']
复制代码
3、根据自界说规则来排序,对元组构成的列表进行排序
  1. tuple_list = [('A', 1,5), ('B', 3,2), ('C', 2,6)]
  2. #key=lambda x: x[1]中可以任意选定x中可选的位置进行排序
  3. sorted(tuple_list, key=lambda x: x[1])
  4. Out[94]: [('A', 1, 5), ('C', 2, 6), ('B', 3, 2)]
  5. sorted(tuple_list, key=lambda x: x[0])
  6. Out[95]: [('A', 1, 5), ('B', 3, 2), ('C', 2, 6)]
  7. sorted(tuple_list, key=lambda x: x[2])
  8. Out[96]: [('B', 3, 2), ('A', 1, 5), ('C', 2, 6)]
复制代码
4、排序的元素是自界说类
  1. class tuple_list:
  2.     def __init__(self, one, two, three):
  3.         self.one = one
  4.         self.two = two
  5.         self.three = three
  6.     def __repr__(self):
  7.         return repr((self.one, self.two, self.three))
  8. tuple_list_ = [tuple_list('A', 1,5), tuple_list('B', 3,2), tuple_list('C', 2,6)]
  9. sorted(tuple_list_, key=lambda x: x.one)
  10. Out[104]: [('A', 1, 5), ('B', 3, 2), ('C', 2, 6)]
  11. sorted(tuple_list_, key=lambda x: x.two)
  12. Out[105]: [('A', 1, 5), ('C', 2, 6), ('B', 3, 2)]
  13. sorted(tuple_list_, key=lambda x: x.three)
  14. Out[106]: [('B', 3, 2), ('A', 1, 5), ('C', 2, 6)]
复制代码
5、sorted 也可以根据多个字段来排序
  1. class tuple_list:
  2.     def __init__(self, one, two, three):
  3.         self.one = one
  4.         self.two = two
  5.         self.three = three
  6.     def __repr__(self):
  7.         return repr((self.one, self.two, self.three))
  8. tuple_list_ = [tuple_list('C', 1,5), tuple_list('A', 3,2), tuple_list('C', 2,6)]
  9. # 首先根据one的位置来排序,然后根据two的位置来排序
  10. sorted(tuple_list_, key=lambda x:(x.one, x.two))
  11. Out[112]: [('A', 3, 2), ('C', 1, 5), ('C', 2, 6)]
复制代码
6、使用operator 中的itemgetter方法和attrgetter方法
  1. tuple_list = [('A', 1,5), ('B', 3,2), ('C', 2,6)]
  2. class tuple_list_class:
  3.     def __init__(self, one, two, three):
  4.         self.one = one
  5.         self.two = two
  6.         self.three = three
  7.     def __repr__(self):
  8.         return repr((self.one, self.two, self.three))
  9. tuple_list_ = [tuple_list_class('C', 1,5), tuple_list_class('A', 3,2), tuple_list_class('C', 2,6)]
  10. from operator import itemgetter
  11. sorted(tuple_list, key=itemgetter(1))
  12. Out[119]: [('A', 1, 5), ('C', 2, 6), ('B', 3, 2)]
  13. from operator import attrgetter
  14. sorted(tuple_list_, key=attrgetter('one')) # attrgetter 传入的参数必须是str
  15. Out[120]: [('A', 3, 2), ('C', 1, 5), ('C', 2, 6)]
  16. # 如果是根据多个类的参数排序,按照参数定义顺序
  17. from operator import attrgetter
  18. sorted(tuple_list_, key=attrgetter('two','one'))
  19. Out[121]: [('C', 1, 5), ('C', 2, 6), ('A', 3, 2)]
复制代码
高级用法

有时间,我们要处理的数据内的元素不是一维的,而是二维的甚至是多维的,那要怎么进行排序呢?这时间,sorted()函数内的key参数就派上用场了!从帮助信息上可以相识到,key参数可传入一个自界说函数。那么,该如何使用呢?让我们看看如下代码:
  1. >>>l=[('a', 1), ('b', 2), ('c', 6), ('d', 4), ('e', 3)]
  2. >>>sorted(l, key=lambda x:x[0])
  3. Out[39]: [('a', 1), ('b', 2), ('c', 6), ('d', 4), ('e', 3)]
  4. >>>sorted(l, key=lambda x:x[0], reverse=True)
  5. Out[40]: [('e', 3), ('d', 4), ('c', 6), ('b', 2), ('a', 1)]
  6. >>>sorted(l, key=lambda x:x[1])
  7. Out[41]: [('a', 1), ('b', 2), ('e', 3), ('d', 4), ('c', 6)]
  8. >>>sorted(l, key=lambda x:x[1], reverse=True)
  9. Out[42]: [('c', 6), ('d', 4), ('e', 3), ('b', 2), ('a', 1)]
复制代码
这里,列表里面的每一个元素都为二维元组,key参数传入了一个lambda函数表达式,其x就代表列表里的每一个元素,然后分别使用索引返回元素内的第一个和第二个元素,这就代表了sorted()函数使用哪一个元素进行排列。而reverse参数就如同上面讲的一样,起到逆排的作用。默认情况下,reverse参数为False。
当然,正如一开始讲到的那样,如果想要对列表直接进行排序操作,可以用成员方法sort()来做:
  1. #学习中遇到问题没人解答?小编创建了一个Python学习交流群:725638078
  2. >>>l.sort(key=lambda x : x[1])
  3. >>>l
  4. Out[45]: [('a', 1), ('b', 2), ('e', 3), ('d', 4), ('c', 6)]
  5. >>>l.sort(key=lambda x : x[1], reverse=True)
  6. >>>l
  7. Out[47]: [('c', 6), ('d', 4), ('e', 3), ('b', 2), ('a', 1)]
复制代码
对于三维及以上的数据排排序,上述方法同样适用。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4