How to create an array which can be used also as a dictionary

Discussion in 'Python' started by Hans Müller, Sep 10, 2009.

  1. Hans Müller

    Hans Müller Guest

    Hello,

    I have a lot of items having a name and a given sequence.

    To access these items fast in a sequence order they should be used as
    a list, but to be fetched fast by name they also should be in a
    dictionary.

    Code could be something like this.

    class item
    def __init__(name, value1, value2, value3):
    self.name = name
    self.value1 = value1
    self.value2 = value2

    a = []
    a.append(item("foo", "bar", "text1"))
    a.append(item("xyz", "basd", "tsddsfxt1"))
    a.append(item("aax", "hello", "dont care"))

    in a, i have my objects in given order, fast accessible by index, e.g.
    a[2] to get the third one. Fine.

    Now I'd like to have a dict with references to thes objects like this:

    d = {}
    for x in a:
    d[a.name] = a # do I get a copy of a here or a new reference ?!

    In d i now have a dict, fast accessible by name.
    But what happens if i modify
    a[1].value1 = 1000
    is
    d["aax"].value1 now 1000 or still "hello" as in this example ?

    Any ideas to get access to the SAME object by index (0..n-1) AND by key ?

    Emphasis here is on speed.


    Thanks a lot,

    Hans
    Hans Müller, Sep 10, 2009
    #1
    1. Advertising

  2. Hans Müller wrote:

    > Hello,
    >
    > I have a lot of items having a name and a given sequence.
    >
    > To access these items fast in a sequence order they should be used as
    > a list, but to be fetched fast by name they also should be in a
    > dictionary.
    >
    > Code could be something like this.
    >
    > class item
    > def __init__(name, value1, value2, value3):
    > self.name = name
    > self.value1 = value1
    > self.value2 = value2
    >
    > a = []
    > a.append(item("foo", "bar", "text1"))
    > a.append(item("xyz", "basd", "tsddsfxt1"))
    > a.append(item("aax", "hello", "dont care"))
    >
    > in a, i have my objects in given order, fast accessible by index, e.g.
    > a[2] to get the third one. Fine.
    >
    > Now I'd like to have a dict with references to thes objects like this:
    >
    > d = {}
    > for x in a:
    > d[a.name] = a # do I get a copy of a here or a new reference ?!


    Only a reference.

    >
    > In d i now have a dict, fast accessible by name.
    > But what happens if i modify
    > a[1].value1 = 1000
    > is
    > d["aax"].value1 now 1000 or still "hello" as in this example ?


    It's changed. Didn't you try that?

    Diez
    Diez B. Roggisch, Sep 10, 2009
    #2
    1. Advertising

  3. Diez B. Roggisch wrote:
    > Hans Müller wrote:
    >
    >> Hello,
    >>
    >> I have a lot of items having a name and a given sequence.
    >>
    >> To access these items fast in a sequence order they should be used as
    >> a list, but to be fetched fast by name they also should be in a
    >> dictionary.
    >>
    >> Code could be something like this.
    >>
    >> class item
    >> def __init__(name, value1, value2, value3):
    >> self.name = name
    >> self.value1 = value1
    >> self.value2 = value2
    >>
    >> a = []
    >> a.append(item("foo", "bar", "text1"))
    >> a.append(item("xyz", "basd", "tsddsfxt1"))
    >> a.append(item("aax", "hello", "dont care"))
    >>
    >> in a, i have my objects in given order, fast accessible by index, e.g.
    >> a[2] to get the third one. Fine.
    >>
    >> Now I'd like to have a dict with references to thes objects like this:
    >>
    >> d = {}
    >> for x in a:
    >> d[a.name] = a # do I get a copy of a here or a new reference ?!

    >
    > Only a reference.

    great, this is in my case what I'm looking for.
    >
    >> In d i now have a dict, fast accessible by name.
    >> But what happens if i modify
    >> a[1].value1 = 1000
    >> is
    >> d["aax"].value1 now 1000 or still "hello" as in this example ?

    >
    > It's changed. Didn't you try that?
    >
    > Diez

    to be true, no - not in this direct context.

    btw. how can I get a copy when I need it ?

    Thanks a lot, Hans
    Hans Müller, Sep 10, 2009
    #3
  4. Hans Müller

    Chris Rebert Guest

    On Thu, Sep 10, 2009 at 12:51 PM, Hans Müller <> wrote:
    > Diez B. Roggisch wrote:
    >> Hans Müller wrote:

    <snip>
    >>> But what happens if i modify
    >>> a[1].value1 = 1000
    >>> is
    >>> d["aax"].value1 now 1000 or still "hello" as in this example ?

    >>
    >> It's changed. Didn't you try that?
    >>

    > to be true, no - not in this direct context.
    >
    > btw. how can I get a copy when I need it ?


    The `copy` module:
    http://docs.python.org/library/copy.html

    Cheers,
    Chris
    --
    http://blog.rebertia.com
    Chris Rebert, Sep 10, 2009
    #4
  5. Hans Müller

    Terry Reedy Guest

    Hans Müller wrote:
    > Hello,
    >
    > I have a lot of items having a name and a given sequence.
    >
    > To access these items fast in a sequence order they should be used as a
    > list, but to be fetched fast by name they also should be in a dictionary.


    Have you looked at namedtuple or OrderedDict in collections module?
    Terry Reedy, Sep 10, 2009
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page