dict duplicity

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Randy Bush

    dict duplicity

    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

  • Paolino

    #2
    Re: dict duplicity

    Randy Bush wrote:[color=blue]
    > 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
    >[/color]
    I'm not sure I got your question but having the same hash(key) is not
    having the same key for a dict.
    [color=blue][color=green][color=darkred]
    >>> {-1:0,-2:0}[/color][/color][/color]
    {-2: 0, -1: 0}[color=blue][color=green][color=darkred]
    >>> hash(-1)!=hash(-2)[/color][/color][/color]
    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

    Comment

    • Randy Bush

      #3
      Re: dict duplicity

      >> a dict written as[color=blue][color=green]
      >>
      >> 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
      >>[/color]
      > I'm not sure I got your question but having the same hash(key) is not
      > having the same key for a dict.
      >[color=green][color=darkred]
      > >>> {-1:0,-2:0}[/color][/color]
      > {-2: 0, -1: 0}[color=green][color=darkred]
      > >>> hash(-1)!=hash(-2)[/color][/color]
      > 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.[/color]

      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

      Comment

      • John Machin

        #4
        Re: dict duplicity

        Randy Bush wrote:[color=blue]
        > 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[/color]

        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.

        [color=blue]
        > 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[/color]

        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>.it eritems()" problem

        Comment

        • Steven Bethard

          #5
          Re: dict duplicity

          Randy Bush wrote:[color=blue]
          > 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=[/color]

          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:


          STeVe

          Comment

          • Randy Bush

            #6
            Re: dict duplicity

            > Firstly, to remove one possible source of confusion, change the name of[color=blue]
            > 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>.it eritems()" problem[/color]

            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

            Comment

            Working...