如何对此列表进行排序,其中最低开始时间首先显示

问题描述 投票:1回答:1

我正在进行课堂作业,当我运行测试人员时,老师给我们输出了一个名为Calendar的字典,其中包含另一个名为Date的字典。在日期内部有开始时间,结束时间和标题,在输出中,最高开始时间首先显示而不是最低开始时间。

我尝试对列表进行排序,但是当我这样做时,它只输出最低的开始日期,而不是第二个日期。

not_date = {
        "start": start_time,
        "end": end_time,
        "title": title
    }
if start_time in range(1, 25):
    if end_time in range(1, 25):
        if start_time <= end_time:
            if date in calendar:
                calendar[date].append(not_date)
                return True
            else:
                calendar[date] = []
                calendar[date].append(not_date)
                return True
        else:
            return False
    else:
        return False
else:
    return False

我们期望这个回报值:

{'2019-07-18': [{'start': 14, 'end': 15, 'title': 'MAT157 Tutorial'}, {'start': 16, 'end': 18, 'title': 'Gym'}]}

但你的代码返回了这个:

{'2019-07-18': [{'start': 16, 'end': 18, 'title': 'Gym'}, {'start': 14, 'end': 15, 'title': 'MAT157 Tutorial'}]}
python
1个回答
2
投票

你的词:

d= {'2019-07-18': [{'start': 14, 'end': 15, 'title': 'MAT157 Tutorial'}, {'start': 16, 'end': 18, 'title': 'Gym'}]}

码:

sorted(d.items(), key = lambda x: x[1], reverse=True)
© www.soinside.com 2019 - 2024. All rights reserved.