gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 23:33:42 24 4
gpt4 key购买 nike

从 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 中禁用 pretty-print 输出选项:

If checked the standard python module pprint is used to format interpreter output.


在选项 > IDE 选项 > Python 解释器下找到它。
pprint module按排序的键顺序输出字典,忽略字典的当前排序。从文档:

Dictionaries are sorted by key before the display is computed.


匹配较旧的 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',
'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() function甚至将其设为默认值:
>>> 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 项目在其控制台实现中使用该选项。

关于python - 为什么 PyScripter 控制台中的输出不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64407966/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com