有没有一种方法可以通过使用Python Lambda降至创建的时间进行排序?

发布于 2025-02-12 03:13:00 字数 551 浏览 1 评论 0原文

我正在与Sqlalchemy合作查询包含事件列表的数据库。每个事件都有创建的时间和一个ARI(亚马逊资源编号)。我可以从一个函数调用中返回最多记录,我想从最新的第一个开始回到记录。为此,我需要手动传递正确对记录的lambda函数;该参数的文档在这里:

cols (default=None):
    A function which takes a DBObject class and returns a tuple of columns (or SQLAlchemy
    expressions) used to compare it. This overrides the default sorting behaviour.
    Example: key=lambda x: (x.created, x.ari)

如您所见,默认情况下,它按X.创建,然后按X.Ari进行升级。实际上,我需要做与此相反的事情,然后按X.创建,然后按x.ari进行排序,以 descing 顺序进行排序。有什么办法可以用lambda做到这一点?使用STROT()或排序()并不是我使用此预先存在的功能的选择。

I'm working with SqlAlchemy to query a database containing a list of events. Each event has a created time and an ari (amazon resource number). There's a maximum number of records I can get back from one function call, and I want to get back the records starting with the most recent first. To do this I need to manually pass in a lambda function that sorts the records properly; documentation for the argument is here:

cols (default=None):
    A function which takes a DBObject class and returns a tuple of columns (or SQLAlchemy
    expressions) used to compare it. This overrides the default sorting behaviour.
    Example: key=lambda x: (x.created, x.ari)

As you can see, by default it sorts by x.created, then x.ari, both in ascending order. I actually need to do the opposite of this and sort by x.created, then x.ari, in descending order. Is there any way I can do this with just a lambda? Using sort() or sorted() is not really an option due to me using this pre-existing function.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文