By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,635 Members | 2,243 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,635 IT Pros & Developers. It's quick & easy.

dict duplicity

P: n/a
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

Aug 18 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Randy Bush wrote:
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.
Regard Paolino
___________________________________
Yahoo! Messenger: chiamate gratuite in tutto il mondo
http://it.beta.messenger.yahoo.com
Aug 18 '05 #2

P: n/a
>> 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

Aug 18 '05 #3

P: n/a
Randy Bush wrote:
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
Note that there are FOUR entries above with the same hash and (more
importantly, as Paolino was trying to drum into you) the same key.

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


Firstly, to remove one possible source of confusion, change the name of
your dictionary ... "mydict" or "fred" ... anything but "dict"

Next, after you have created the dictionary and added items to it, do this:
print len(fred)
print len(fred.items())
nitems = 0
for k, v in fred.iteritems():
nitems += 1
print nitems

If by this stage you haven't worked out what you are doing wrong, post
an exact copy/paste of the MINIMAL code that exhibits the "multiple
instances of same key in <dictionary>.iteritems()" problem
Aug 19 '05 #4

P: n/a
Randy Bush wrote:
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=


Whatever the hash function is for your keys, it returns the same value
for two different keys. A simple example of this kind of phenomenon:

py> class C(object):
.... def __hash__(self):
.... return 1
....
py> c1, c2 = C(), C()
py> d = {c1:1, c2:2}
py> for key, value in d.iteritems():
.... print hash(key), key, value
....
1 <__main__.C object at 0x0126CCD0> 1
1 <__main__.C object at 0x012739D0> 2

Note that the hashes are the same. But are the objects the same?

py> c1 == c2
False

Nope. (This is because the default __eq__ method compares the ids of
the two objects, and as you can see, they have different ids.)

The point is this: you can have multiple objects in the same dict that
have the same hash(), as long as none of those objects equals another.
For more information, see:
http://wiki.python.org/moin/DictionaryKeys

STeVe
Aug 19 '05 #5

P: n/a
> Firstly, to remove one possible source of confusion, change the name of
your dictionary ... "mydict" or "fred" ... anything but "dict"

Next, after you have created the dictionary and added items to it, do this:
print len(fred)
print len(fred.items())
nitems = 0
for k, v in fred.iteritems():
nitems += 1
print nitems

If by this stage you haven't worked out what you are doing wrong, post
an exact copy/paste of the MINIMAL code that exhibits the "multiple
instances of same key in <dictionary>.iteritems()" problem


i did not work out what i was doing wrongly. but now it works. i
hate <bleep>ing magic!

but thanks for making me permute.

randy

Aug 19 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.