About the output format. - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: About the output format. (/thread-15257.html) |
About the output format. - shang2019 - Jan-10-2019 I'm a newer to python, just have some simple format questions. Here is my code of creating a new function. [python] >>> def make_complex(x,y): ... return{'y':y,'x':x} >>> make_complex(4,2) {'y': 2, 'x': 4} [python] and the second version is like this: [python] >>> def make_complex(x,y): ... return{'x':x,'y':y,} >>> make_complex(4,2) {'y': 2, 'x': 4} [python] I found out that for the return part, I won't get the real order of what I define in the function. It turns out 'y' always be the first one to show up. Could anyone clear my confusion? Thanks. RE: About the output format. - woooee - Jan-10-2019 Dictionary keys are hashed, so they are in "hashed" order, not input order. Use an OrderedDict if you want keys in original order. The latest version of Python uses an OrderedDict by default so it also depends on which version of Python you have installed. RE: About the output format. - buran - Jan-10-2019 (Jan-10-2019, 06:03 AM)woooee Wrote: Dictionary keys are hashed, so they are in "hashed" order, not input order. Your statement suggest there is some kind of order that is preserved, even if it is not the order of insertion. That's not true for python version before 3.6. There is no guarantee that any order will be preserved between different runs of the same script, i.e. if you run the same script multiple times you may get different order. This is more easy to observe with larger dicts. In python 3.6. order of insertion is preserved, but it is considered implementation detail and should no be relied upon. As of 3.7. order of insertion preservation feature is part of official python API. However is not correct to say that (Jan-10-2019, 06:03 AM)woooee Wrote: the latest version of Python uses an OrderedDict by default. RE: About the output format. - shang2019 - Jan-10-2019 (Jan-10-2019, 07:23 AM)buran 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?(Jan-10-2019, 06:03 AM)woooee Wrote: Dictionary keys are hashed, so they are in "hashed" order, not input order. RE: About the output format. - buran - Jan-10-2019 (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 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. |