Python Forum
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
About the output format.
#5
(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

Reply


Messages In This Thread
About the output format. - by shang2019 - Jan-10-2019, 05:35 AM
RE: About the output format. - by woooee - Jan-10-2019, 06:03 AM
RE: About the output format. - by buran - Jan-10-2019, 07:23 AM
RE: About the output format. - by shang2019 - Jan-10-2019, 06:01 PM
RE: About the output format. - by buran - Jan-10-2019, 07:38 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  format the output from a nested dictionary. nostradamus64 9 4,598 May-03-2021, 04:45 PM
Last Post: nostradamus64
  JupyterLab Dictionary Content Output Format Ourkid123uk 0 1,325 Sep-04-2020, 02:18 PM
Last Post: Ourkid123uk
  Format SQL Query Output phillyfa 2 4,069 Apr-22-2020, 07:45 AM
Last Post: buran
  Save output into a Excel Sheet with Format Table skaailet 1 2,512 Apr-17-2020, 11:56 PM
Last Post: thirteendec
  Display output in readable format and save hnkrish 1 2,640 Jul-19-2019, 09:29 AM
Last Post: Larz60+
  python script to get wildcard mask output in the following format techrichit 0 3,835 Aug-10-2018, 11:01 PM
Last Post: techrichit

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020