Ordering python sets

Peter Otten __peter__ at web.de
Wed Oct 22 15:52:13 EDT 2008


Duncan Booth wrote:

> Peter Otten <__peter__ at web.de> wrote:
> 
>> Here's another one:
>> 
>>>>> set([1,9])
>> set([1, 9])
>>>>> set([9,1])
>> set([9, 1])
>> 
>> This time I did indeed search systematically...
>> 
> You missed one with smaller values:
>>>> set([8,0])
> set([8, 0])
>>>> set([0,8])
> set([0, 8])

I searched the minimal combination with one...

> You can work some of it out quite easily instead of searching. The hash
> value for an integer is itself, the initial space allocated for the set is
> 8 slots so each value is reduced modulo 8. If the values you insert don't
> clash then the order of insertion doesn't matter. If there are values
> which coincide on the same slot then the second one inserted will go into
> a different slot so the order may vary.

I guess I have to move the goal posts to beat you:

>>> set([-1,-2]), set([-2,-1])
(set([-2, -1]), set([-1, -2]))

For that one the number of slots doesn't matter because

>>> hash(-1), hash(-2)
(-2, -2)

Peter



More information about the Python-list mailing list