如何按值对字典进行排序

时间:2021-05-09 18:43:43

标签: python

我正在尝试按值对字典进行排序,该值是格式为 H:MM:SS(例如“0:41:42”)的时间戳,但下面的代码没有按预期工作:

album_len = {
    'The Piper At The Gates Of Dawn': '0:41:50',
    'A Saucerful of Secrets': '0:39:23',
    'More': '0:44:53', 'Division Bell': '1:05:52',
    'The Wall': '1:17:46',
    'Dark side of the moon': '0:45:18',
    'Wish you were here': '0:44:17',
    'Animals': '0:41:42'
}
album_len = OrderedDict(sorted(album_len.items()))

这是我得到的输出:

OrderedDict([
    ('A Saucerful of Secrets', '0:39:23'),
    ('Animals', '0:41:42'),
    ('Dark side of the moon', '0:45:18'),
    ('Division Bell', '1:05:52'),
    ('More', '0:44:53'),
    ('The Piper At The Gates Of Dawn', '0:41:50'),
    ('The Wall', '1:17:46'),
    ('Wish you were here', '0:44:17')])

它不应该是那样的。我希望看到的第一个元素是 ('The Wall', '1:17:46'),最长的一个。

如何按照我想要的方式对元素进行排序?

2 个答案:

答案 0 :(得分:3)

尝试将每个值转换为日期时间并将其用作键:

<html>
    <head>
        <link rel="stylesheet" href="homepage.css">
    </head>
    <header class="header">
        <img class="logo" />
        <div class="profile">
            <img class="profilepic" />
        <label id="name">ABCD</label>
        </div>
    </header>
    <body>
    
    </body>
    </html>

输出:

from collections import OrderedDict
from datetime import datetime


def convert_to_datetime(val):
    return datetime.strptime(val, "%H:%M:%S")


album_len = {'The Piper At The Gates Of Dawn': '0:41:50',
             'A Saucerful of Secrets': '0:39:23', 'More': '0:44:53',
             'Division Bell': '1:05:52', 'The Wall': '1:17:46',
             'Dark side of the moon': '0:45:18',
             'Wish you were here': '0:44:17', 'Animals': '0:41:42'}
album_len = OrderedDict(
    sorted(album_len.items(), key=lambda i: convert_to_datetime(i[1]))
)
print(album_len)

或者以降序将 reverse 设置为 True:

OrderedDict([('A Saucerful of Secrets', '0:39:23'), ('Animals', '0:41:42'),
             ('The Piper At The Gates Of Dawn', '0:41:50'),
             ('Wish you were here', '0:44:17'), ('More', '0:44:53'),
             ('Dark side of the moon', '0:45:18'), ('Division Bell', '1:05:52'),
             ('The Wall', '1:17:46')])

输出:

album_len = OrderedDict(
    sorted(
        album_len.items(),
        key=lambda i: convert_to_datetime(i[1]),
        reverse=True
    )
)

编辑:如果只需要维护插入顺序并且不使用 OrderedDict([('The Wall', '1:17:46'), ('Division Bell', '1:05:52'), ('Dark side of the moon', '0:45:18'), ('More', '0:44:53'), ('Wish you were here', '0:44:17'), ('The Piper At The Gates Of Dawn', '0:41:50'), ('Animals', '0:41:42'), ('A Saucerful of Secrets', '0:39:23')]) 等特定的 OrderedDict 函数,那么常规 python move_to_end 也适用于 Python3.7+。

升序:

dict

降序:

album_len = dict(
    sorted(album_len.items(), key=lambda i: convert_to_datetime(i[1]))
)

答案 1 :(得分:1)

这是问题的副本:How do I sort a dictionary by value?"

>>> dict(sorted(album_len.items(), key=lambda item: item[1]))
{'A Saucerful of Secrets': '0:39:23',
 'Animals': '0:41:42',
 'The Piper At The Gates Of Dawn': '0:41:50',
 'Wish you were here': '0:44:17',
 'More': '0:44:53',
 'Dark side of the moon': '0:45:18',
 'Division Bell': '1:05:52',
 'The Wall': '1:17:46'}

注意:时间格式已经是按字典序排列的,不需要转成日期时间。
请参阅下面的@DarrylG 评论。他是完全正确的,因此,只要持续时间不超过 9:59:59,词典顺序上的注释就有效,除非小时数用前导零填充。

相关问题