我想排序OrderedDict OrderedDict通過'深度'鍵。 有沒有解決方案來排序字典?如何排序OrderedDict OrderedDict - Python
OrderedDict([
(2, OrderedDict([
('depth', 0),
('height', 51),
('width', 51),
('id', 100)
])),
(1, OrderedDict([
('depth', 2),
('height', 51),
('width', 51),
('id', 55)
])),
(0, OrderedDict([
('depth', 1),
('height', 51),
('width', 51),
('id', 48)
])),
])
排序字典應該是這樣的:
OrderedDict([
(2, OrderedDict([
('depth', 0),
('height', 51),
('width', 51),
('id', 100)
])),
(0, OrderedDict([
('depth', 1),
('height', 51),
('width', 51),
('id', 48)
])),
(1, OrderedDict([
('depth', 2),
('height', 51),
('width', 51),
('id', 55)
])),
])
任何想法如何得到它?
如果任何字典沒有「深度」作爲關鍵字,這將導致異常。這可能是需要的。如果不是,則可以通過使用「get」來假定默認密鑰。 – TomOnTime
請注意,這裏'[1]'是指foo的值,而不是它的鍵,它將是'[0]' – emisilva