2012年4月30日 星期一

Python 筆記 namedtuple and sort with lambda expression



'''
Created on 2012-5-1

@author: Maca
'''

from collections import namedtuple

People = namedtuple("People",["Name","Age"])
Lists = [People("Maca","24"),People("Puma","18"),People("Lula","30")]

print Lists

Lists.sort(key=lambda args:args.Age)

print Lists

Lists.sort(key=lambda args:args.Name)

print Lists



Outputs:

[People(Name='Maca', Age='24'), People(Name='Puma', Age='18'), People(Name='Lula', Age='30')]
[People(Name='Puma', Age='18'), People(Name='Maca', Age='24'), People(Name='Lula', Age='30')]
[People(Name='Lula', Age='30'), People(Name='Maca', Age='24'), People(Name='Puma', Age='18')]


沒有留言:

張貼留言