【发布时间】:2021-01-07 02:03:06
【问题描述】:
我在 python 中有一个字典
data={'d':{'2022-01-04':'completed'},'b':{'2020-12-04':'not competed'}}
我想按日期排序此列表 所以结果应该是这样的
data={'b':{'2020-12-04':'not competed'},'d':{'2022-01-04':'completed'}}
试过
{i:sorted(data[i].items(), key=lambda x: x[0]) for i in data}
但它只对子字典进行排序,知道怎么做吗?
【问题讨论】:
-
你只是告诉它对子
dict进行排序。 -
子字典会一直只有一个键值对吗?
-
在 python 中,
dict没有排序。使用OrderedDict或list或tuples。
标签: python dictionary nested