https://docs.python.org/2/howto/sorting.html
1. 基本排序
mylist = [5, 2, 3, 1, 4]
print(sorted(mylist))
[1, 2, 3, 4, 5]
返回新的有序的list,当然你也可以使用list的sort方法,list的sort方法改变原始的list,并且返回None
mylist = [5, 2, 3, 1, 4]
mylist.sort()
print(mylist)
[1, 2, 3, 4, 5]
list的sort方法只能应用在list上,但是sorted方法接受任何可迭代的对象,比如:
mylist = sorted({1: 'D', 2: 'B', 3: 'B', 4: 'E', 5: 'A'})
print(mylist)
[1, 2, 3, 4, 5]
2. key参数
sorted定义了key参数,这个参数需要传递一个method对象
mylist = sorted("This is a test string from Andrew".split(),key=str.lower)
print(mylist)
['a', 'Andrew', 'from', 'is', 'string', 'test', 'This']
使用对象索引排序:
student_tuples = [
('john', 'A', 15),
('jane', 'B', 12),
('dave', 'B', 10),
]
mylist = sorted(student_tuples, key=lambda student: student[2])
print(mylist)
[('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),
]
mylist = sorted(student_objects, key=lambda student: student.age)
print(mylist)
[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
3 Operator模块
from operator import itemgetter, attrgetter
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),
]
student_tuples = [
('john', 'A', 15),
('jane', 'B', 12),
('dave', 'B', 10),
]
mylist = sorted(student_objects, key = attrgetter('age'))
print(mylist)
mylist2 = sorted(student_tuples, key = itemgetter(2))
print(mylist2)
[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
多个维度排序:
from operator import itemgetter, attrgetter
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),
]
student_tuples = [
('john', 'A', 15),
('jane', 'B', 12),
('dave', 'B', 10),
]
mylist = sorted(student_objects, key = attrgetter('grade','age'))
print(mylist)
mylist2 = sorted(student_tuples, key = itemgetter(1,2))
print(mylist2)
[('john', 'A', 15), ('dave', 'B', 10), ('jane', 'B', 12)]
[('john', 'A', 15), ('dave', 'B', 10), ('jane', 'B', 12)]
使用operator.methodcaller()调用对象的方法:调用字符串的count方法
from operator import methodcaller
messages = ['critical!!!', 'hurry!', 'standby', 'immediate!!']
mylist = sorted(messages, key=methodcaller('count', '!'))
print(mylist)
['standby', 'hurry!', 'immediate!!', 'critical!!!']
4. 升序降序
使用reserve参数,降序排序
from operator import itemgetter, attrgetter
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),
]
student_tuples = [
('john', 'A', 15),
('jane', 'B', 12),
('dave', 'B', 10),
]
mylist = sorted(student_objects, key = attrgetter('age'),reverse= True)
print(mylist)
mylist2 = sorted(student_tuples, key = itemgetter(2),reverse = True)
print(mylist2)
[('john', 'A', 15), ('jane', 'B', 12), ('dave', 'B', 10)]
[('john', 'A', 15), ('jane', 'B', 12), ('dave', 'B', 10)]
稳定的排序算法,也就是说如果相同的对象,还是原来的顺序,原来谁在前排序后还是谁在前
data = [('red', 1), ('blue', 1), ('red', 2), ('blue', 2)]
mylist = sorted(data, key=itemgetter(0))
print(mylist)
[('blue', 1), ('blue', 2), ('red', 1), ('red', 2)]