OrderedDict不保存订单 [英] OrderedDict does not preserve the order

查看:98
本文介绍了OrderedDict不保存订单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

from collections import OrderedDict
import pprint

menu = {"about" : "about", "login" : "login", 'signup': "signup"}

menu = OrderedDict(menu)
pprint.pprint(menu.items())

import sys
sys.exit()






输出为: p>


The output is:

[('about', 'about'), ('signup', 'signup'), ('login', 'login')]

所以,即使使用 OrderedDict 。我知道字典默认情况下不保留初始顺序,而且所有这些东西。但是我想知道为什么 OrderedDict 不起作用。

So, the order is not preserved even with the use of OrderedDict. I know the dictionaries don't preserve the initial order by default, and all those things. But I want to learn why the OrderedDict is not working.

推荐答案

通过将这些项目放在(非有序)的命令中,并从中构建OrderedDict,您已经丢弃了原始的命令。从元组列表中构造OrderedDict,而不是dict。

By putting the items in a (non-ordered) dict and constructing the OrderedDict from that, you've already discarded the original order. Construct the OrderedDict from a list of tuples, not a dict.

这篇关于OrderedDict不保存订单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆