python 容器排序
2014-01-13 12:10
316 查看
本文主要是对下面链接的翻译和概括http://docs.python.org/2/howto/sorting.html
通常简单的调用sorted已经可以满足大多数的需求,但是对于复杂的数据结构,以及多层次优先级的组合排序和稳定性的需求,我们就要使用sorted更高级别的设置,sorted提供了自定义关键字和比较函数的方式来满足这些要求。
sorted函数用法如下:
>>>
上面写lambda可能有点麻烦,python还提供了更简单的办法,另外这个办法还可以按照优先级对不同关键字组合排序
>>>
>>>
>>>
但是如果还要兼容过去的版本可以使用下面的办法将cmp转换成key
To convert to a key function, just wrap the old comparison function:
>>>
>>>
通常简单的调用sorted已经可以满足大多数的需求,但是对于复杂的数据结构,以及多层次优先级的组合排序和稳定性的需求,我们就要使用sorted更高级别的设置,sorted提供了自定义关键字和比较函数的方式来满足这些要求。
sorted函数用法如下:
sorted(data, cmp=None, key=None, reverse=False)
排序基础
>>> sorted([5, 2, 3, 1, 4]) [1, 2, 3, 4, 5]
>>> a = [5, 2, 3, 1, 4] >>> a.sort() >>> a [1, 2, 3, 4, 5]
>>> sorted({1: 'D', 2: 'B', 3: 'B', 4: 'E', 5: 'A'}) [1, 2, 3, 4, 5]
自定义关键字排序
注意key是一个接受一个参数,然后将其转化成一个可以比较的简单数据的函数,如下面 str.lower就是一个函数>>> sorted("This is a test string from Andrew".split(), key=str.lower) ['a', 'Andrew', 'from', 'is', 'string', 'test', 'This']
>>> student_tuples = [ ('john', 'A', 15), ('jane', 'B', 12), ('dave', 'B', 10), ] >>> sorted(student_tuples, key=lambda student: student[2]) # sort by age [('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
>>> class Student: def __init__(self, name, grade, age): self.name = name self.grade = grade self.age = age def __repr__(self): return repr((self.name, self.grade, self.age))
>>>
>>> student_objects = [ Student('john', 'A', 15), Student('jane', 'B', 12), Student('dave', 'B', 10), ] >>> sorted(student_objects, key=lambda student: student.age) # sort by age [('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
上面写lambda可能有点麻烦,python还提供了更简单的办法,另外这个办法还可以按照优先级对不同关键字组合排序
>>> from operator import itemgetter, attrgetter
>>>
>>> sorted(student_tuples, key=itemgetter(2)) [('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
>>>
>>> sorted(student_objects, key=attrgetter('age')) [('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
>>> sorted(student_tuples, key=itemgetter(1,2)) [('john', 'A', 15), ('dave', 'B', 10), ('jane', 'B', 12)]
>>>
>>> sorted(student_objects, key=attrgetter('grade', 'age')) [('john', 'A', 15), ('dave', 'B', 10), ('jane', 'B', 12)]
自定义比较函数排序
这个在3.0之后不建议使用>>> def numeric_compare(x, y): return x - y >>> sorted([5, 2, 4, 1, 3], cmp=numeric_compare) [1, 2, 3, 4, 5]
>>> def reverse_numeric(x, y): return y - x >>> sorted([5, 2, 4, 1, 3], cmp=reverse_numeric) [5, 4, 3, 2, 1]
但是如果还要兼容过去的版本可以使用下面的办法将cmp转换成key
def cmp_to_key(mycmp): 'Convert a cmp= function into a key= function' class K(object): def __init__(self, obj, *args): self.obj = obj def __lt__(self, other): return mycmp(self.obj, other.obj) < 0 def __gt__(self, other): return mycmp(self.obj, other.obj) > 0 def __eq__(self, other): return mycmp(self.obj, other.obj) == 0 def __le__(self, other): return mycmp(self.obj, other.obj) <= 0 def __ge__(self, other): return mycmp(self.obj, other.obj) >= 0 def __ne__(self, other): return mycmp(self.obj, other.obj) != 0 return K
To convert to a key function, just wrap the old comparison function:
>>>
>>> sorted([5, 2, 4, 1, 3], key=cmp_to_key(reverse_numeric)) [5, 4, 3, 2, 1]
升序和降序
>>> sorted(student_tuples, key=itemgetter(2), reverse=True) [('john', 'A', 15), ('jane', 'B', 12), ('dave', 'B', 10)]
>>>
>>> sorted(student_objects, key=attrgetter('age'), reverse=True) [('john', 'A', 15), ('jane', 'B', 12), ('dave', 'B', 10)]
排序的稳定性
这个不需要我们额外设置,python所选用的排序算法都是稳定的。相关文章推荐
- python-各类容器的复制和排序问题
- 『Python CoolBook:heapq』数据结构和算法_heapq堆队列算法&容器排序
- python容器排序sort()和内建函数sorted()的区别
- PythonStock(5):使用 dataTable+python 实现服务端多字段排序
- 插入排序 @ Python
- Python 容器(Collections)
- python 排序
- Java 容器排序
- python几个排序函数 sort sorted argsort
- 关于python的列表排序问题汇总
- [python]一行搞定字符串排序
- 【Python排序搜索基本算法】之无向图的最小割&Karger算法(Graphs and Minimum Cuts & Karger's Min-Cut Algorithm)
- Java容器排序的方法
- java vector等容器排序
- 使用python 分析统计nginx访问日志ip次数并且排序
- python字典实现按照自定义顺序排序
- 对python字典的排序
- 小记 用python进行排序
- leetcode—217. Contains Duplicate 查找双数,python排序sort、sorted,for遍历的范围
- 快速排序-降序(python)