(Jan-10-2019, 06:01 PM)shang2019 Wrote: So, is that means, actually, the order is not preserved by different python version and there is no good reason to explain the order of the output as you state that you may get different order running the same script?
from the docs
Quote:Note
By default, the __hash__() values of str, bytes and datetime objects are “salted” with an unpredictable random value. Although they remain constant within an individual Python process, they are not predictable between repeated invocations of Python.
This is intended to provide protection against a denial-of-service caused by carefully-chosen inputs that exploit the worst case performance of a dict insertion, O(n^2) complexity. See http://www.ocert.org/advisories/ocert-2011-003.html for details.
Changing hash values affects the iteration order of sets. Python has never made guarantees about this ordering (and it typically varies between 32-bit and 64-bit builds).
See also PYTHONHASHSEED.
Also, check that SO answer
https://stackoverflow.com/a/15479974/4046632
Quote:Note that as of Python 3.3, a random hash seed is used as well, making hash collisions unpredictable to prevent certain types of denial of service (where an attacker renders a Python server unresponsive by causing mass hash collisions). This means that the order of a given dictionary is then also dependent on the random hash seed for the current Python invocation.
That's a result from a change in python 3.3 that hash randomization is switched on by default as a security feature. If you want more info on hash randomization check https://stackoverflow.com/a/27522708/4046632
As already stated - as of 3.7 Insertion order preservation is part of the API.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs