Jea*_*bre 3 python dictionary pyscripter
从 PyScripter (3.6.4.0) REPL 控制台:
*** Python 3.7.7 (tags/v3.7.7:d7c567b08f, Mar 10 2020, 10:41:24) [MSC v.1900 64 bit (AMD64)] on win32. ***
*** Remote Python engine is active ***
>>> d = {}
>>> d['B'] = 12
>>> d['A'] = 10
>>> d['C'] = 34
>>> d
{'A': 10, 'B': 12, 'C': 34}
Run Code Online (Sandbox Code Playgroud)
这个结果让我们相信 Python 对键进行排序并且不保留插入顺序,而从 3.6 版本开始就可以保证。
现在让我们在 PyScripter 之外的控制台中运行完全相同版本的 Python:
Python 3.7.7 (tags/v3.7.7:d7c567b08f, Mar 10 2020, 10:41:24) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> d = {}
>>> d['B'] = 12
>>> d['A'] = 10
>>> d['C'] = 34
>>> d
{'B': 12, 'A': 10, 'C': 34}
Run Code Online (Sandbox Code Playgroud)
插入顺序保存好。
为什么输出不同?
在 pyscripter REPL 中,当我不依赖 REPL 提供答案而是使用print语句时,我得到了正确的结果:
>>> print(d)
{'B': 12, 'A': 10, 'C': 34}
Run Code Online (Sandbox Code Playgroud)
where 只d产生键,就好像它们被排序一样:
>>> d
{'A': 10, 'B': 12, 'C': 34}
Run Code Online (Sandbox Code Playgroud)
因此,当涉及到字典顺序时,不要相信 pyscripter REPL 输出。
您需要在 pyscripter 中禁用Pretty print 输出选项:
\n\n\n如果选中,则标准 python 模块 pprint 用于格式化解释器输出。
\n
在选项 > IDE 选项 > Python 解释器下找到它。
\n该pprint模块按排序的键顺序输出字典,忽略字典的当前顺序。从文档中:
\n\n在计算显示之前,字典按键排序。
\n
It\xe2\x80\x99s 不是匹配旧 Python 版本输出的 hack,因为在 Python 3.6 之前,顺序取决于插入和删除顺序加上随机哈希种子。
\n相反,使用pprint当输出变得笨拙时,通过使用换行符和缩进,使用可以提供更好的输出,其中标准表示将所有内容放在一行上。
你的具体例子并没有真正体现出差异,更长的字典会让它更清楚:
\n>>> from requests import get\n>>> from secrets import token_hex\n>>> from pprint import pprint\n>>> fortunes = {token_hex(4): get("https://fortuneapi.herokuapp.com/").text for _ in range(5)}\n>>> print(repr(fortunes)) # standard REPL output prints the repr() result\n{\'a33435f0\': \'"If reporters don\\\'t know that truth is plural, they ought to be lawyers.\\\\n\\\\t\\\\t-- Tom Wicker\\\\n"\\n\', \'1f08db3c\': \'"Very few profundities can be expressed in less than 80 characters.\\\\n"\\n\', \'6037e01e\': \'"The main problem I have with cats is, they\\\'re not dogs.\\\\n\\\\t\\\\t-- Kevin Cowherd\\\\n"\\n\', \'b817eaf8\': \'"New York now leads the world\\\'s great cities in the number of people around\\\\nwhom you shouldn\\\'t make a sudden move.\\\\n\\\\t\\\\t-- David Letterman\\\\n"\\n\', \'c89994e7\': \'"I\\\'m GLAD I remembered to XEROX all my UNDERSHIRTS!!\\\\n"\\n\'}\n>>> pprint(fortunes) # pprint outputs pretty-printed lines, sorted.\n{\'1f08db3c\': \'"Very few profundities can be expressed in less than 80 \'\n \'characters.\\\\n"\\n\',\n \'6037e01e\': \'"The main problem I have with cats is, they\\\'re not \'\n \'dogs.\\\\n\\\\t\\\\t-- Kevin Cowherd\\\\n"\\n\',\n \'a33435f0\': \'"If reporters don\\\'t know that truth is plural, they ought to be \'\n \'lawyers.\\\\n\\\\t\\\\t-- Tom Wicker\\\\n"\\n\',\n \'b817eaf8\': \'"New York now leads the world\\\'s great cities in the number of \'\n "people around\\\\nwhom you shouldn\'t make a sudden "\n \'move.\\\\n\\\\t\\\\t-- David Letterman\\\\n"\\n\',\n \'c89994e7\': \'"I\\\'m GLAD I remembered to XEROX all my UNDERSHIRTS!!\\\\n"\\n\'}\nRun Code Online (Sandbox Code Playgroud)\n如果您只是偶尔需要检查字典的特定项目顺序,您可以启用该选项,然后使用print(dictionary)在特殊情况下使用。
您还可以将sort_dicts=False参数传递给pprint(),前提是您手动调用它;功能pprint.pp()甚至将其设为默认值:
>>> from pprint import pp\n>>> pp(fortunes)\n{\'a33435f0\': \'"If reporters don\\\'t know that truth is plural, they ought to be \'\n \'lawyers.\\\\n\\\\t\\\\t-- Tom Wicker\\\\n"\\n\',\n \'1f08db3c\': \'"Very few profundities can be expressed in less than 80 \'\n \'characters.\\\\n"\\n\',\n \'6037e01e\': \'"The main problem I have with cats is, they\\\'re not \'\n \'dogs.\\\\n\\\\t\\\\t-- Kevin Cowherd\\\\n"\\n\',\n \'b817eaf8\': \'"New York now leads the world\\\'s great cities in the number of \'\n "people around\\\\nwhom you shouldn\'t make a sudden "\n \'move.\\\\n\\\\t\\\\t-- David Letterman\\\\n"\\n\',\n \'c89994e7\': \'"I\\\'m GLAD I remembered to XEROX all my UNDERSHIRTS!!\\\\n"\\n\'\n}\nRun Code Online (Sandbox Code Playgroud)\n或者您可以要求 PyScripter 项目在其控制台实现中使用该选项。
\n