我有一个航班列表,这些航班将首先在出发日期排序,然后按总航班持续时间排序,并根据日期保留订单。
我试过了:
sorted(flights, key=methodcaller('date','flighttime'))
但methodcaller只接受1个参数。我首先尝试了groupby,然后对航班时间进行排序,但之后列表只被分类到飞行时间。
谢谢
答案 0 :(得分:2)
methodcaller
不能调用多于1个方法,其他参数是参数:
f = methodcaller(' name',' foo',bar = 1),调用f(b)返回b.name(' foo',巴= 1)
所以可以使用methodcaller
完成,但可能更复杂,可能涉及lambda
喜欢(未经测试)lambda x : methodcaller('name')(x),methodcaller('flighttime')(x)
所以我会改用简单的lambda
(其中x
是Flight
个对象):
sorted(flights, key=lambda x : (x.date(),x.flighttime()))