标签:
面试题之一。
s=[
{"name":"Axx","score":"90"},
{"name":"Bxx","score":"91"},
{"name":"Cxx","score":"20"},
]
请用一行代码对上述list,按照score排序。
s=[ {"name":"Axx","score":"90"}, {"name":"Bxx","score":"91"}, {"name":"Cxx","score":"20"}, ] print "original s: ",s new_s = sorted(s,key = lambda e:e.__getitem__('score')) print "new s: ",new_s
original s: [{‘score‘: ‘90‘, ‘name‘: ‘Axx‘}, {‘score‘: ‘91‘, ‘name‘: ‘Bxx‘}, {‘score‘: ‘20‘, ‘name‘: ‘Cxx‘}]
new s: [{‘score‘: ‘20‘, ‘name‘: ‘Cxx‘}, {‘score‘: ‘90‘, ‘name‘: ‘Axx‘}, {‘score‘: ‘91‘, ‘name‘: ‘Bxx‘}]
吐槽:print字典的排序和我s的定义顺序貌似不一样,我定义是name,score,print出来是score再name。貌似也无关紧要,dic本来就无序。
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/emaste_r/article/details/47373011