source: http://www.cnblogs.com/rubylouvre/archive/2011/06/19/2084801.html
sorted 是 python的内置函数,可以用来对列表和字典进行排序。
以下是两个例子:
1、对列表排序
>>>
elist = [ 1 , 5 , 3 , 7 ] >>> sorted (elist) [ 1 , 3 , 5 , 7 ] |
2、对字典排序
>>>
edict = { "ok" : 1 , "no" : 2 } >>> sorted (edict.items(),key = lambda d:
d[ 0 ]) [( 'no' , 2 ),
( 'ok' , 1 )] |
在python中,sorted定义如下:
sorted( iterable[, cmp[, key[, reverse]]]) iterable:是可迭代类型类型; cmp:用于比较的函数,比较什么由key决定,有默认值,迭代集合中的一项; key:用列表元素的某个属性和函数进行作为关键字,有默认值,迭代集合中的一项; reverse:排序规则. reverse = True 或者 reverse = False,有默认值。 返回值:是一个经过排序的可迭代类型,与iterable一样。
一般来说,cmp和key可以使用lambda表达式。
下面对例子2进行解析。
edict.items() 对字典项进行排序
key=lambda d: d[0] 用字典的key进行排序。注意,d代表了字典集合中的一项。d[0]即是字典项的key.
结果大家都看到了。但是如果,将这个表达式改为key = lambda d:d[1]呢?
>>>a = [ 5 , 2 , 1 , 9 , 6 ] >>> sorted (a) #将a从小到大排序,不影响a本身结构 >>> sorted (a,reverse = True ) #将a从大到小排序,不影响a本身结构 >>>a.sort() #将a从小到大排序,影响a本身结构 >>>a.sort(reverse = True ) #将a从大到小排序,影响a本身结构 >>>
c = [ 'CCC' , 'bb' , 'ffff' , 'z' ] >>> sorted (c,key = len ) #按列表的元素的长度排序 [ 'z' , 'bb' , 'CCC' , 'ffff' ] >>>
d = [ 'CCC' , 'bb' , 'ffff' , 'z' ] >>> sorted (d,key = str .lower
) #将列表中的每个元素变为小写,再按每个元素中的每个字母的ascii码从小到大排序 [ 'bb' , 'CCC' , 'ffff' , 'z' ] >>> def lastchar(s): return s[ - 1 ] >>>
e = [ 'abc' , 'b' , 'AAz' , 'ef' ] >>> sorted (e,key = lastchar) #自定义函数排序,lastchar为函数名,这个函数返回列表e中每个元素的最后一个字母 [ 'b' , 'abc' , 'ef' , 'AAz' ] #sorted(e,key=lastchar)作用就是
按列表e中每个元素的最后一个字母的ascii码从小到大排序 >>>
f = [{ 'name' : 'abc' , 'age' : 20 },{ 'name' : 'def' , 'age' : 30 },{ 'name' : 'ghi' , 'age' : 25 }] #列表中的元素为字典 >>> def age(s): return s[ 'age' ] >>>
ff = sorted (f,key = age) #自定义函数按列表f中字典的age从小到大排序 [{ 'age' : 20 , 'name' : 'abc' },
{ 'age' : 25 , 'name' : 'ghi' },
{ 'age' : 30 , 'name' : 'def' }] >>>
f2 = sorted (f,key = lambda x:x[ 'age' ]) #如果觉得上面定义一个函数代码不美观,可以用lambda的形式来定义函数,效果同上
python 列表(list)和字典(dict)数据排序 source:http://www.douban.com/note/13460891/2008-06-20 09:38:01
# sort.py
# 这个类用来演示如何对自定义对象进行排序 class Sortobj: a = 0 b = '' def __init__(self, a, b): self.a = a self.b = b def printab(self): print self.a, self.b # 演示对字符串列表进行排序 samplelist_str = ['blue','allen','sophia','keen'] print samplelist_str samplelist_str.sort() print samplelist_str print '\n' # 演示对整型数进行排序 samplelist_int = [34,23,2,2333,45] print samplelist_int samplelist_int.sort() print samplelist_int print '\n' # 演示对字典数据进行排序 sampledict_str = {'blue':'5555@sina.com', 'allen':'222@163.com', 'sophia':'4444@gmail.com', 'ceen':'blue@263.net'} print sampledict_str # 按照key进行排序 print sorted(sampledict_str.items(), key=lambda d: d[0]) # 按照value进行排序 print sorted(sampledict_str.items(), key=lambda d: d[1]) # 构建用于排序的类实例 obja = Sortobj(343, 'keen') objb = Sortobj(56, 'blue') objc = Sortobj(2, 'aba') objd = Sortobj(89, 'iiii') print '\n' samplelist_obj = [obja, objb, objc, objd] # 实例对象排序前 for obj in samplelist_obj: obj.printab() print '\n' # 按照对象的a属性进行排序 samplelist_obj.sort(lambda x,y: cmp(x.a, y.a)) for obj in samplelist_obj: obj.printab() print '\n' # 按照对象的b属性进行排序 samplelist_obj.sort(lambda x,y: cmp(x.b, y.b)) for obj in samplelist_obj: obj.printab() |