dict() 函数在 colab 和 jupyter notebook 之间的行为不同
dict() function behaves differently between colab and jupyter notebook
Google 的 colab 和 jupyter notebook 对 dict()
函数的反应不同
Jupyter 笔记本:
!python --version
Python 3.7.6
代码:
trial=[(1, 250), (3, 275), (5, 290), (2, 300), (4, 500)]
dict(trial)
输出:
{1: 250, 3: 275, 5: 290, 2: 300, 4: 500}
Google Colab:
!python --version
Python 3.6.9
代码:
trial=[(1, 250), (3, 275), (5, 290), (2, 300), (4, 500)]
dict(trial)
输出:
{1: 250, 2: 300, 3: 275, 4: 500, 5: 290}
你觉得为什么会有这样的差异,会不会也是版本问题?
从python 3.7开始,字典保持键的顺序与插入的顺序相同,所以回答你的问题,是的,这是因为版本。
这个想法是从 python3.6 开始提出的,但仍然被认为是 implementation dependent:
The order-preserving aspect of this new implementation is considered an implementation detail and should not be relied upon (this may change in the future, but it is desired to have this new dict implementation in the language for a few releases before changing the language spec to mandate order-preserving semantics for all current and future Python implementations; this also helps preserve backwards-compatibility with older versions of the language where random iteration order is still in effect, e.g. Python 3.5).
从 Python 3.7 开始,此要求是强制性的,并已提升为语言规范。
Google colab python 现在是版本 3.7.12,行为仍然相同,所以@Jarvis 的回答是不正确的。
Google colab python 以正确的顺序在内部存储字典,并且在使用打印功能显示时使用正确的顺序。但是,使用 colab 显示单元格中最后一个表达式的值时,它会按键对字典进行排序。我的建议是始终使用 print() 函数。
代码:
trial=[(1, 250), (3, 275), (5, 290), (2, 300), (4, 500)]
dct_trial = dict(trial)
print(dct_trial)
dct_trial
输出:(注意顺序有何不同)
{1: 250, 3: 275, 5: 290, 2: 300, 4: 500}
{1: 250, 2: 300, 3: 275, 4: 500, 5: 290}
Google 的 colab 和 jupyter notebook 对 dict()
函数的反应不同
Jupyter 笔记本:
!python --version
Python 3.7.6
代码:
trial=[(1, 250), (3, 275), (5, 290), (2, 300), (4, 500)]
dict(trial)
输出:
{1: 250, 3: 275, 5: 290, 2: 300, 4: 500}
Google Colab:
!python --version
Python 3.6.9
代码:
trial=[(1, 250), (3, 275), (5, 290), (2, 300), (4, 500)]
dict(trial)
输出:
{1: 250, 2: 300, 3: 275, 4: 500, 5: 290}
你觉得为什么会有这样的差异,会不会也是版本问题?
从python 3.7开始,字典保持键的顺序与插入的顺序相同,所以回答你的问题,是的,这是因为版本。
这个想法是从 python3.6 开始提出的,但仍然被认为是 implementation dependent:
The order-preserving aspect of this new implementation is considered an implementation detail and should not be relied upon (this may change in the future, but it is desired to have this new dict implementation in the language for a few releases before changing the language spec to mandate order-preserving semantics for all current and future Python implementations; this also helps preserve backwards-compatibility with older versions of the language where random iteration order is still in effect, e.g. Python 3.5).
从 Python 3.7 开始,此要求是强制性的,并已提升为语言规范。
Google colab python 现在是版本 3.7.12,行为仍然相同,所以@Jarvis 的回答是不正确的。
Google colab python 以正确的顺序在内部存储字典,并且在使用打印功能显示时使用正确的顺序。但是,使用 colab 显示单元格中最后一个表达式的值时,它会按键对字典进行排序。我的建议是始终使用 print() 函数。
代码:
trial=[(1, 250), (3, 275), (5, 290), (2, 300), (4, 500)]
dct_trial = dict(trial)
print(dct_trial)
dct_trial
输出:(注意顺序有何不同)
{1: 250, 3: 275, 5: 290, 2: 300, 4: 500}
{1: 250, 2: 300, 3: 275, 4: 500, 5: 290}