繁体   English   中英

为什么 PyScripter 控制台中的输出不同?

[英]Why is the output different in PyScripter console?

从 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}

这个结果让我们相信 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}

插入顺序保存好。

为什么输出不同?

在 pyscripter REPL 中,当我不依赖 REPL 提供答案而是使用print语句时,我得到了正确的结果:

>>> print(d)
{'B': 12, 'A': 10, 'C': 34}

其中d产生键,就像它们被排序一样:

>>> d
{'A': 10, 'B': 12, 'C': 34}

因此,当涉及到字典顺序时,不要相信 pyscripter REPL 输出。

您需要在 pyscripter 中禁用漂亮的打印输出选项:

如果选中,则标准 python 模块 pprint 用于格式化解释器输出。

Options > IDE Options > Python Interpreter下找到它。

pprint模块以排序的键顺序输出字典,忽略字典的当前排序。 从文档:

字典在计算显示之前按关键字排序。

匹配较旧的 Python 版本输出并不是一个技巧,因为在 Python 3.6 之前, 顺序取决于插入和删除顺序以及随机散列种子

相反,通过使用换行符和缩进,当输出变得笨拙时,使用pprint为您提供更好的输出,其中标准表示会将所有内容放在一行中。

你的具体例子并没有真正体现出差异,更长的字典会让它更清楚:

>>> from requests import get
>>> from secrets import token_hex
>>> from pprint import pprint
>>> fortunes = {token_hex(4): get("https://fortuneapi.herokuapp.com/").text for _ in range(5)}
>>> print(repr(fortunes))  # standard REPL output prints the repr() result
{'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'}
>>> pprint(fortunes)  # pprint outputs pretty-printed lines, sorted.
{'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',
 'a33435f0': '"If reporters don\'t know that truth is plural, they ought to be '
             'lawyers.\\n\\t\\t-- Tom Wicker\\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'}

如果您只是偶尔需要检查字典的特定项目顺序,您可以只启用该选项,并在特殊情况下使用print(dictionary)

您还可以将sort_dicts=False参数传递给pprint() ,前提是您手动调用它; pprint.pp()函数甚至将其pprint.pp()默认值:

>>> from pprint import pp
>>> pp(fortunes)
{'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'
}

或者您可以要求 PyScripter 项目在其控制台实现中使用该选项。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM