我有发票清单,其中包括发票对象。 我想根据它们的日期和用途来订购这些物品。你知道吗
from operator import attrgetter
invoices_list.sort(key=attrgetter('date'))
这就是我犯的错误。你知道吗
TypeError: can't compare FakeDatetime to NoneType
我想做升序的对象根据日期和无日期应该是第一个。那么其他人应该以升序来。你知道吗
$ invoices_list[0].date
$ FakeDatetime(2015, 7, 3, 0, 0)
一个简单的密钥包装器将完成这项工作:
然后使用它:
编写一个自定义比较函数,该函数知道如何比较
FakeDateTime
和None
对象,然后通过指定cmp
关键字参数来告诉sort()
使用此函数。你知道吗如果您为None设置了一些默认值(比如0),您可以执行以下操作:
相关问题 更多 >
编程相关推荐