dictionaries/pointers

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rob Conner

    #1

    dictionaries/pointers

    I dont know how to do this and can't think of a simple way to.

    All I want is a dictionary where two keys point to the same object.
    (to steal the ascii art from
    http://starship.python.net/crew/mwh/...jectthink.html)
    I want sometihng like this:

    ,------. +-------+
    | dict |------>|+-----+| +---+
    `------' || "a" |+---->| 1 |
    |+-----+| +---+
    | | ^
    |+-----+| |
    || "b" |+-------'
    |+-----+|
    +-------+
    | |
    |+-----+| +---+
    || "c" |+---->| 2 |
    |+-----+| +---+
    +-------+

    Where if I change "a" or "b" to 3 the other one will change?
    Is this even possible? How would I do it?

  • Erik Max Francis

    #2
    Re: dictionaries/pointers

    Rob Conner wrote:
    [color=blue]
    > I dont know how to do this and can't think of a simple way to.
    >
    > All I want is a dictionary where two keys point to the same object.
    > (to steal the ascii art from
    > http://starship.python.net/crew/mwh/...jectthink.html)
    > I want sometihng like this:
    >
    > ,------. +-------+
    > | dict |------>|+-----+| +---+
    > `------' || "a" |+---->| 1 |
    > |+-----+| +---+
    > | | ^
    > |+-----+| |
    > || "b" |+-------'
    > |+-----+|
    > +-------+
    > | |
    > |+-----+| +---+
    > || "c" |+---->| 2 |
    > |+-----+| +---+
    > +-------+
    >
    > Where if I change "a" or "b" to 3 the other one will change?
    > Is this even possible? How would I do it?[/color]

    Objects of type int are immutable in Python, so you'll need a helper
    class to do this. Try something like:
    [color=blue][color=green][color=darkred]
    >>> class Container:[/color][/color][/color]
    .... def __init__(self, value=None): self.value = value
    .... def get(self): return self.value
    .... def set(self, value): self.value = value
    ....[color=blue][color=green][color=darkred]
    >>> one = Container(1)
    >>> myDictionary = {}
    >>> myDictionary['a'] = one
    >>> myDictionary['b'] = one
    >>> myDictionary['b'].set(3)
    >>> print myDictionary['a'].get()[/color][/color][/color]
    3

    --
    Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
    San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
    Never make a promise or plan / Take a little love where you can
    -- Florence, _Chess_

    Comment

    • Dave Hansen

      #3
      Re: dictionaries/pointers

      On 7 Oct 2005 14:23:49 -0700, "Rob Conner" <rtconner@gmail .com> wrote:
      [color=blue]
      >I dont know how to do this and can't think of a simple way to.
      >
      >All I want is a dictionary where two keys point to the same object.
      >(to steal the ascii art from
      >http://starship.python.net/crew/mwh/...jectthink.html)
      >I want sometihng like this:
      >
      >,------. +-------+
      >| dict |------>|+-----+| +---+
      >`------' || "a" |+---->| 1 |
      > |+-----+| +---+
      > | | ^
      > |+-----+| |
      > || "b" |+-------'
      > |+-----+|
      > +-------+
      > | |
      > |+-----+| +---+
      > || "c" |+---->| 2 |
      > |+-----+| +---+
      > +-------+
      >
      >Where if I change "a" or "b" to 3 the other one will change?
      >Is this even possible? How would I do it?[/color]

      A simple, ugly answer: Use a mutable object rather than a plain
      integer. Example:
      [color=blue][color=green][color=darkred]
      >>> elt = [1]
      >>> dict = {"a":elt, "b":elt, "c":[2]}
      >>> print dict[/color][/color][/color]
      {'a': [1], 'c': [2], 'b': [1]}[color=blue][color=green][color=darkred]
      >>> dict["a"][0] = 3
      >>> print dict[/color][/color][/color]
      {'a': [3], 'c': [2], 'b': [3]}[color=blue][color=green][color=darkred]
      >>>[/color][/color][/color]

      Regards,

      -=Dave
      --
      Change is inevitable, progress is not.

      Comment

      Working...