python之sort高级用法
2015-05-08 13:30
288 查看
both list.sort() and sorted() added a key parameter to specify a FUNCTION to be called on each list element prior to making comparisons.
而且通过通过设置reverse可以颠倒排序结果
#注意key后的是function,不是function call的值, 所以常用lambda表达式
sorted("This is a test string".split(), key=str.lower)
----------------------------------------
['This', 'test', 'string', 'is', 'a']
#A common pattern is to sort complex objects using some of the object’s indices as keys
student_tuples = [('john', 'A', 15),('jane', 'B', 12),('dave', 'B', 10),]
sorted(student_tuples, key=lambda student: student[2])
----------------------------------------
[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
#The same technique works for objects with named attributes
class Student(object):
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)
------------------------------------------
[('Dave', 'B', 10), ('Jane', 'B', 12), ('John', 'A', 15)]
#上面都可以一些python内置的函数
from operator import itemgetter, attrgetter
sorted(student_tuples, key=itemgetter(2))
sorted(student_objects, key=attrgetter('age'))
sorted(student_tuples, key=itemgetter(1,2))
sorted(student_objects, key=attrgetter('grade', 'age'))
而且通过通过设置reverse可以颠倒排序结果
#注意key后的是function,不是function call的值, 所以常用lambda表达式
sorted("This is a test string".split(), key=str.lower)
----------------------------------------
['This', 'test', 'string', 'is', 'a']
#A common pattern is to sort complex objects using some of the object’s indices as keys
student_tuples = [('john', 'A', 15),('jane', 'B', 12),('dave', 'B', 10),]
sorted(student_tuples, key=lambda student: student[2])
----------------------------------------
[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
#The same technique works for objects with named attributes
class Student(object):
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)
------------------------------------------
[('Dave', 'B', 10), ('Jane', 'B', 12), ('John', 'A', 15)]
#上面都可以一些python内置的函数
from operator import itemgetter, attrgetter
sorted(student_tuples, key=itemgetter(2))
sorted(student_objects, key=attrgetter('age'))
sorted(student_tuples, key=itemgetter(1,2))
sorted(student_objects, key=attrgetter('grade', 'age'))
相关文章推荐
- python sort、sorted 高级用法
- linux关于sort命令的高级用法(按多个列值进行排列)
- [Python] Numpy array 高级用法(3)
- python中List的sort方法的用法
- Python爬虫 Urllib库的高级用法
- python学习笔记-(14)python循环中的高级用法
- python sort、sorted高级排序技巧
- 转 Python爬虫入门四之Urllib库的高级用法
- python sort、sorted高级排序技巧
- Python爬虫(3):Requests的高级用法
- [Python] Numpy array 高级用法(4)
- Python高级用法综合举例
- python requests 高级用法 -- 包括SSL 证书错误的解决方案
- Python 高级用法
- python字典sort和min函数的用法
- Python编程_Lesson016_类的高级用法
- 介绍Python的Urllib库的一些高级用法
- 介绍Python的Urllib库的一些高级用法
- Sort命令高级用法:对多列进行排序
- 【Python】 sort、sorted高级排序技巧