学生先按名称排序,在按成绩排序
from operator import itemgetter
students=[(‘Bob‘,90),(‘David‘,99),(‘Dacheng‘,96),(‘Max‘,94)]
sorted(students,key=itemgetter(0))
sorted(students, key=lambda t: t[1])
#sorted(students,key=itemgetter(1))
关于itemgetter函数:operator.itemgetter函数获取的不是值,而是定义了一个函数,通过该函数作用到对象上才能获取值。
a = [1,2,3]
>>> b=operator.itemgetter(1) //定义函数b,获取对象的第1个域的值
>>> b(a)
2
>>> b=operator.itemgetter(1,0) //定义函数b,获取对象的第1个域和第0个的值
>>> b(a)
(2, 1)
原文:http://www.cnblogs.com/daacheng/p/7818812.html
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!