微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决为什么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}

该结果使我们相信Python对键进行排序,并且不保留插入顺序,而从3.6版开始可以保证。

现在让我们在PyScripter外部的控制台中运行完全相同的Python版本:

Python 3.7.7 (tags/v3.7.7:d7c567b08f,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来格式化解释器输出。

选项> IDE选项> Python解释器下找到它。

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

在计算显示内容之前,字典将按键排序。

匹配旧的Python版本输出不是黑客,因为在Python 3.6之前的order depended on insertion and deletion order plus a randomised hash seed

相反,使用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',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() function甚至将其设置为默认值:

>>> from pprint import pp
>>> pp(fortunes)
{'a33435f0': '"If reporters don\'t know that truth is plural,'1f08db3c': '"Very few profundities can be expressed in less than 80 '
             'characters.\\n"\n','c89994e7': '"I\'m GLAD I remembered to XEROX all my UNDERSHIRTS!!\\n"\n'
}

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。