当前位置: 移动技术网 > IT编程>脚本编程>Python > Python sorted函数详解(高级篇)

Python sorted函数详解(高级篇)

2018年09月21日  | 移动技术网IT编程  | 我要评论

于谦老婆白慧明,春暖花开论坛公告,精美qq签名

sorted 用于对集合进行排序(这里集合是对可迭代对象的一个统称,他们可以是列表、字典、set、甚至是字符串),它的功能非常强大

1、对列表排序,返回的对象不会改变原列表

list = [1,5,7,2,4]

sorted(list)
out[87]: [1, 2, 4, 5, 7]
#可以设定时候排序方式,默认从小到大,设定reverse = false 可以从大到小
sorted(list,reverse=false)
out[88]: [1, 2, 4, 5, 7]

sorted(list,reverse=true)
out[89]: [7, 5, 4, 2, 1]

2、根据自定义规则来排序,使用参数:key

# 使用key,默认搭配lambda函数使用
sorted(chars,key=lambda x:len(x))
out[92]: ['a', 'is', 'boy', 'bruce', 'handsome']

sorted(chars,key=lambda x:len(x),reverse= true)
out[93]: ['handsome', 'bruce', 'boy', 'is', 'a']

3、根据自定义规则来排序,对元组构成的列表进行排序

tuple_list = [('a', 1,5), ('b', 3,2), ('c', 2,6)]
#key=lambda x: x[1]中可以任意选定x中可选的位置进行排序
sorted(tuple_list, key=lambda x: x[1]) 

out[94]: [('a', 1, 5), ('c', 2, 6), ('b', 3, 2)]

sorted(tuple_list, key=lambda x: x[0])
out[95]: [('a', 1, 5), ('b', 3, 2), ('c', 2, 6)]

sorted(tuple_list, key=lambda x: x[2])
out[96]: [('b', 3, 2), ('a', 1, 5), ('c', 2, 6)]

4、排序的元素是自定义类

class tuple_list:
 def __init__(self, one, two, three):
  self.one = one
  self.two = two
  self.three = three
 def __repr__(self):
  return repr((self.one, self.two, self.three))


tuple_list_ = [tuple_list('a', 1,5), tuple_list('b', 3,2), tuple_list('c', 2,6)]

sorted(tuple_list_, key=lambda x: x.one)
out[104]: [('a', 1, 5), ('b', 3, 2), ('c', 2, 6)]

sorted(tuple_list_, key=lambda x: x.two)
out[105]: [('a', 1, 5), ('c', 2, 6), ('b', 3, 2)]

sorted(tuple_list_, key=lambda x: x.three)
out[106]: [('b', 3, 2), ('a', 1, 5), ('c', 2, 6)]

5、sorted 也可以根据多个字段来排序

class tuple_list:
 def __init__(self, one, two, three):
  self.one = one
  self.two = two
  self.three = three
 def __repr__(self):
  return repr((self.one, self.two, self.three))

tuple_list_ = [tuple_list('c', 1,5), tuple_list('a', 3,2), tuple_list('c', 2,6)]
# 首先根据one的位置来排序,然后根据two的位置来排序
sorted(tuple_list_, key=lambda x:(x.one, x.two))
out[112]: [('a', 3, 2), ('c', 1, 5), ('c', 2, 6)]

6、使用operator 中的itemgetter方法和attrgetter方法

tuple_list = [('a', 1,5), ('b', 3,2), ('c', 2,6)]

class tuple_list_class:
 def __init__(self, one, two, three):
  self.one = one
  self.two = two
  self.three = three
 def __repr__(self):
  return repr((self.one, self.two, self.three))

tuple_list_ = [tuple_list_class('c', 1,5), tuple_list_class('a', 3,2), tuple_list_class('c', 2,6)]

from operator import itemgetter
sorted(tuple_list, key=itemgetter(1))

out[119]: [('a', 1, 5), ('c', 2, 6), ('b', 3, 2)]

from operator import attrgetter
sorted(tuple_list_, key=attrgetter('one')) # attrgetter 传入的参数必须是str

out[120]: [('a', 3, 2), ('c', 1, 5), ('c', 2, 6)]

# 如果是根据多个类的参数排序,按照参数定义顺序
from operator import attrgetter
sorted(tuple_list_, key=attrgetter('two','one'))

out[121]: [('c', 1, 5), ('c', 2, 6), ('a', 3, 2)]

高级用法

有时候,我们要处理的数据内的元素不是一维的,而是二维的甚至是多维的,那要怎么进行排序呢?这时候,sorted()函数内的key参数就派上用场了!从帮助信息上可以了解到,key参数可传入一个自定义函数。那么,该如何使用呢?让我们看看如下代码:

>>>l=[('a', 1), ('b', 2), ('c', 6), ('d', 4), ('e', 3)]
>>>sorted(l, key=lambda x:x[0])
out[39]: [('a', 1), ('b', 2), ('c', 6), ('d', 4), ('e', 3)]
>>>sorted(l, key=lambda x:x[0], reverse=true)
out[40]: [('e', 3), ('d', 4), ('c', 6), ('b', 2), ('a', 1)]
>>>sorted(l, key=lambda x:x[1])
out[41]: [('a', 1), ('b', 2), ('e', 3), ('d', 4), ('c', 6)]
>>>sorted(l, key=lambda x:x[1], reverse=true)
out[42]: [('c', 6), ('d', 4), ('e', 3), ('b', 2), ('a', 1)]

这里,列表里面的每一个元素都为二维元组,key参数传入了一个lambda函数表达式,其x就代表列表里的每一个元素,然后分别利用索引返回元素内的第一个和第二个元素,这就代表了sorted()函数利用哪一个元素进行排列。而reverse参数就如同上面讲的一样,起到逆排的作用。默认情况下,reverse参数为false。
当然,正如一开始讲到的那样,如果想要对列表直接进行排序操作,可以用成员方法sort()来做:

>>>l.sort(key=lambda x : x[1])
>>>l
out[45]: [('a', 1), ('b', 2), ('e', 3), ('d', 4), ('c', 6)]
>>>l.sort(key=lambda x : x[1], reverse=true)
>>>l
out[47]: [('c', 6), ('d', 4), ('e', 3), ('b', 2), ('a', 1)]

对于三维及以上的数据排排序,上述方法同样适用。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对移动技术网的支持。如果你想了解更多相关内容请查看下面相关链接

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网