dict duplicity

Randy Bush randy at psg.com
Thu Aug 18 17:42:22 EDT 2005


>> a dict written as
>> 
>>    pKey = (prefix, pLen, origin)
>> 
>>    val = dict.get(pKey)
>>    if val == None:
>>       dict[pKey] = (timeB, timeB)
>>    else:
>>       if val[0]> timeB:  val[0] = timeB
>>       if val[1] < timeB:  val[1] = timeB
>>       dict[pKey] = val
>> 
>> and read back as
>> 
>>    for pKey, pVal in dict.iteritems():
>>       print \
>>          pKey[0], hash(pKey[0]), \
>>          pKey[1], hash(pKey[1]), \
>>          pKey[2], hash(pKey[2]), \
>>          "hash=", hash(pKey), \
>>          pVal[0], hash(pVal[0]), \
>>          pVal[1], hash(pVal[1])
>> 
>> when run with | sort, produces
>> 
>> 12.0.0.0 -2054516913 8 8 7018 329707286 hash= -604503432 917088000 917088000 917088000 917088000
>> 12.0.0.0 -2054516913 8 8 7018 329707286 hash= -604503432 917088000 917088000 917088000 917088000
>> 12.0.0.0 -2054516913 8 8 7018 329707286 hash= -604503432 917088000 917088000 917088000 917088000
>> 12.0.0.0 -2054516913 8 8 7018 329707286 hash= -604503432 917088000 917088000 917088000 917088000
>> 12.0.0.0 -2054516913 9 -1293912648 7018 329707286 hash= -1578430040 917088000 917088000 917088000 917088000
>> 12.0.0.0 -2054516913 9 -1293912648 7018 329707286 hash= -1578430040 917088000 917088000 917088000 917088000
>> 
>> not that there are two entries with the same hash=
>> 
>> i am utterly confused
>> 
>> randy
>> 
> I'm not sure I got your question but having the same hash(key) is not 
> having the same key for a dict.
> 
> >>> {-1:0,-2:0}
> {-2: 0, -1: 0}
> >>> hash(-1)!=hash(-2)
> False
> 
> A key lookup in a dict involve real keys comparisons via '==' among the 
> keys of the bin identified by the hash of the key.

ack.  my point was the key touple and its hash are identical for
each pair of entries.

i executed the write section 55953 times.  the iteritems gets me
111906 entries.

while it did not charge extra, it kinda spoils my code :-)

randy




More information about the Python-list mailing list