So I decided to write a little test program to run on a variety of
CPythons, to confirm what I was thinking.
And instead I got a surprise.
On 1.4 through 2.1 I got descending key order. I expected the keys to be scattered, but they weren't.
On 2.2 through 3.5 I got ascending key order. I expected the keys to be scattered, but they weren't.
On 3.6 through 3.10 I got insertion order, as expected.
But why are 1.4 through 3.5 ordering so much?
So I decided to write a little test program to run on a variety of CPythons, to confirm what I was thinking.
And instead I got a surprise.
On 1.4 through 2.1 I got descending key order. I expected the keys to be scattered, but they weren't.
On 2.2 through 3.5 I got ascending key order. I expected the keys to be scattered, but they weren't.
On 3.6 through 3.10 I got insertion order, as expected.
But why are 1.4 through 3.5 ordering so much?
That's long in the past, but I seem to recall that key order was
unspecified. That would give the implementer (likely Tim Peters much of the time) the freedom to do whatever worked best for performance or simplicity
of implementation.
keys = [5, 10, 15, 14, 9, 4, 1, 2, 8, 6, 7, 12, 11]
dict_ = {}
for key in keys:
dict_[key] = 1
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19][hash(x) for x in range(20)]
On 1.4 through 2.1 I got descending key order. I expected the keys to be scattered, but they weren't.
On 2022-08-01 at 13:41:11 -0700,
Dan Stromberg <drsalists@gmail.com> wrote:
keys = [5, 10, 15, 14, 9, 4, 1, 2, 8, 6, 7, 12, 11]
dict_ = {}
for key in keys:
dict_[key] = 1
$ python
Python 3.10.5 (main, Jun 6 2022, 18:49:26) [GCC 12.1.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19][hash(x) for x in range(20)]
Just sayin'. :-)
On Tue, 2 Aug 2022 at 07:48, <2QdxY4RzWzUUiLuE@potatochowder.com> wrote:
On 2022-08-01 at 13:41:11 -0700,
Dan Stromberg <drsalists@gmail.com> wrote:
keys = [5, 10, 15, 14, 9, 4, 1, 2, 8, 6, 7, 12, 11]
dict_ = {}
for key in keys:
dict_[key] = 1
$ python
Python 3.10.5 (main, Jun 6 2022, 18:49:26) [GCC 12.1.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19][hash(x) for x in range(20)]
Just sayin'. :-)
Yes, but I'm pretty sure that's been true for a LONG time. The hashes
for small integers have been themselves for as long as I can remember.
But the behaviour of the dictionary, when fed such keys, is what's
changed.
On 2022-08-02 at 07:50:52 +1000,
Chris Angelico <rosuav@gmail.com> wrote:
On Tue, 2 Aug 2022 at 07:48, <2QdxY4RzWzUUiLuE@potatochowder.com> wrote:
On 2022-08-01 at 13:41:11 -0700,
Dan Stromberg <drsalists@gmail.com> wrote:
keys = [5, 10, 15, 14, 9, 4, 1, 2, 8, 6, 7, 12, 11]
dict_ = {}
for key in keys:
dict_[key] = 1
$ python
Python 3.10.5 (main, Jun 6 2022, 18:49:26) [GCC 12.1.0] on linux
Type "help", "copyright", "credits" or "license" for more information. >>> [hash(x) for x in range(20)]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
Just sayin'. :-)
Yes, but I'm pretty sure that's been true for a LONG time. The hashes
for small integers have been themselves for as long as I can remember.
But the behaviour of the dictionary, when fed such keys, is what's
changed.
I'm not disputing either of those facts. I'm pointing out that the apparently arbitrary order of a mapping's keys becomes obvious when you
look at the hashes of those keys.
Yes, but I'm pretty sure that's been true for a LONG time. The hashes
for small integers have been themselves for as long as I can remember.
But the behaviour of the dictionary, when fed such keys, is what's
changed.
I'm not disputing either of those facts. I'm pointing out that the
apparently arbitrary order of a mapping's keys becomes obvious when you
look at the hashes of those keys.
It looks like the relationship no longer holds at around keys = list(range(250, 260))
But i == hash(i) holds for the first million values at least.
What am I missing?
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 293 |
Nodes: | 16 (2 / 14) |
Uptime: | 218:09:40 |
Calls: | 6,621 |
Calls today: | 3 |
Files: | 12,171 |
Messages: | 5,317,781 |