Python列表按降序排序

发布于 2024-10-02 02:26:20 字数 420 浏览 6 评论 0原文

如何按降序排列此列表?

timestamps = [
    "2010-04-20 10:07:30",
    "2010-04-20 10:07:38",
    "2010-04-20 10:07:52",
    "2010-04-20 10:08:22",
    "2010-04-20 10:08:22",
    "2010-04-20 10:09:46",
    "2010-04-20 10:10:37",
    "2010-04-20 10:10:58",
    "2010-04-20 10:11:50",
    "2010-04-20 10:12:13",
    "2010-04-20 10:12:13",
    "2010-04-20 10:25:38"
]

How can I sort this list in descending order?

timestamps = [
    "2010-04-20 10:07:30",
    "2010-04-20 10:07:38",
    "2010-04-20 10:07:52",
    "2010-04-20 10:08:22",
    "2010-04-20 10:08:22",
    "2010-04-20 10:09:46",
    "2010-04-20 10:10:37",
    "2010-04-20 10:10:58",
    "2010-04-20 10:11:50",
    "2010-04-20 10:12:13",
    "2010-04-20 10:12:13",
    "2010-04-20 10:25:38"
]

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

一张白纸 2024-10-09 02:26:21

这将为您提供数组的排序版本。

sorted(timestamps, reverse=True)

如果您想就地排序:

timestamps.sort(reverse=True)

请查看文档 排序方法

This will give you a sorted version of the array.

sorted(timestamps, reverse=True)

If you want to sort in-place:

timestamps.sort(reverse=True)

Check the docs at Sorting HOW TO

决绝 2024-10-09 02:26:21

在一行中,使用 lambda :

timestamps.sort(key=lambda x: time.strptime(x, '%Y-%m-%d %H:%M:%S')[0:6], reverse=True)

将函数传递给 list.sort :

def foo(x):
    return time.strptime(x, '%Y-%m-%d %H:%M:%S')[0:6]

timestamps.sort(key=foo, reverse=True)

In one line, using a lambda:

timestamps.sort(key=lambda x: time.strptime(x, '%Y-%m-%d %H:%M:%S')[0:6], reverse=True)

Passing a function to list.sort:

def foo(x):
    return time.strptime(x, '%Y-%m-%d %H:%M:%S')[0:6]

timestamps.sort(key=foo, reverse=True)
月朦胧 2024-10-09 02:26:21

你可以简单地这样做:

timestamps.sort(reverse=True)

You can simply do this:

timestamps.sort(reverse=True)
掌心的温暖 2024-10-09 02:26:21

你简单的输入:

timestamps.sort()
timestamps=timestamps[::-1]

you simple type:

timestamps.sort()
timestamps=timestamps[::-1]
我为君王 2024-10-09 02:26:21

由于您的列表已经按升序排列,因此我们可以简单地反转列表。

>>> timestamps.reverse()
>>> timestamps
['2010-04-20 10:25:38', 
'2010-04-20 10:12:13', 
'2010-04-20 10:12:13', 
'2010-04-20 10:11:50', 
'2010-04-20 10:10:58', 
'2010-04-20 10:10:37', 
'2010-04-20 10:09:46', 
'2010-04-20 10:08:22',
'2010-04-20 10:08:22', 
'2010-04-20 10:07:52', 
'2010-04-20 10:07:38', 
'2010-04-20 10:07:30']

Since your list is already in ascending order, we can simply reverse the list.

>>> timestamps.reverse()
>>> timestamps
['2010-04-20 10:25:38', 
'2010-04-20 10:12:13', 
'2010-04-20 10:12:13', 
'2010-04-20 10:11:50', 
'2010-04-20 10:10:58', 
'2010-04-20 10:10:37', 
'2010-04-20 10:09:46', 
'2010-04-20 10:08:22',
'2010-04-20 10:08:22', 
'2010-04-20 10:07:52', 
'2010-04-20 10:07:38', 
'2010-04-20 10:07:30']
两仪 2024-10-09 02:26:21

这是另一种方式


timestamps.sort()
timestamps.reverse()
print(timestamps)

Here is another way


timestamps.sort()
timestamps.reverse()
print(timestamps)
伴我老 2024-10-09 02:26:21

特别是如果数据是数字的话,可以使用否定来按降序排序。如果您无论如何都需要传递排序键,这尤其有用。例如,如果数据如下:

data = ['9', '10', '3', '4.5']
sorted(data, reverse=True)                      # doesn't sort correctly
sorted(data, key=lambda x: -float(x))           # sorts correctly
#                          ^ negate here

# that said, passing a key along with reverse=True also work
sorted(data, key=float, reverse=True)           # ['10', '9', '4.5', '3']

对于日期时间的示例,则如下所示:

from datetime import datetime
ts = ["04/20/2010 10:07:30", "12/01/2009 10:07:52", "01/13/2020 10:08:22", "12/01/2009 12:07:52"]
ts.sort(key=lambda x: -datetime.strptime(x, '%m/%d/%Y %H:%M:%S').timestamp())
#                                                               ^^^^ convert to a number here
ts
# ['01/13/2020 10:08:22', '04/20/2010 10:07:30', '12/01/2009 12:07:52', '12/01/2009 10:07:52']

Especially if the data is numeric, negation can be used to sort in descending order. This is especially useful if you need to pass a sorting key anyway. For example, if the data was as follows:

data = ['9', '10', '3', '4.5']
sorted(data, reverse=True)                      # doesn't sort correctly
sorted(data, key=lambda x: -float(x))           # sorts correctly
#                          ^ negate here

# that said, passing a key along with reverse=True also work
sorted(data, key=float, reverse=True)           # ['10', '9', '4.5', '3']

For an example with datetime, that would look like as follows:

from datetime import datetime
ts = ["04/20/2010 10:07:30", "12/01/2009 10:07:52", "01/13/2020 10:08:22", "12/01/2009 12:07:52"]
ts.sort(key=lambda x: -datetime.strptime(x, '%m/%d/%Y %H:%M:%S').timestamp())
#                                                               ^^^^ convert to a number here
ts
# ['01/13/2020 10:08:22', '04/20/2010 10:07:30', '12/01/2009 12:07:52', '12/01/2009 10:07:52']
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文