complex data types?

Discussion in 'Python' started by richard, Sep 18, 2005.

  1. richard

    richard Guest

    I'd like to have an array in which the elements are various data types.
    How do I do this in Python?

    For example:

    array[0].artist = 'genesis'
    array[0].album = 'foxtrot'
    array[0].songs = ['watcher', 'time table', 'friday']
    array[1].artist = 'beatles'
    array[1].album = 'abbey road'
    array[1].songs = ['come', 'something', 'maxwell']

    Everything I try generates errors or results in array[0].songs equaling
    array[1].songs. I feel I'm missing something obvious.
    richard, Sep 18, 2005
    #1
    1. Advertising

  2. richard

    Guest

    richard wrote:
    > I'd like to have an array in which the elements are various data types.
    > How do I do this in Python?
    >
    > For example:
    >
    > array[0].artist = 'genesis'
    > array[0].album = 'foxtrot'
    > array[0].songs = ['watcher', 'time table', 'friday']
    > array[1].artist = 'beatles'
    > array[1].album = 'abbey road'
    > array[1].songs = ['come', 'something', 'maxwell']
    >
    > Everything I try generates errors or results in array[0].songs equaling
    > array[1].songs. I feel I'm missing something obvious.


    If you show us where array[0] and array[1] are being initialized, it
    would be easier to diagnose the problem.

    At the same time, if array[0].songs equals array[1] songs, you are
    probably initializing both array[0] and array[1] with the same object.
    Since array[0] and array[1] both refer to the same object, a change to
    one will be reflected in the other.

    I hope this is what you're looking for.

    Michael Loritsch
    , Sep 18, 2005
    #2
    1. Advertising

  3. On Sat, 2005-09-17 at 20:49 -0500, richard wrote:
    > I'd like to have an array in which the elements are various data types.
    > How do I do this in Python?
    >
    > For example:
    >
    > array[0].artist = 'genesis'
    > array[0].album = 'foxtrot'
    > array[0].songs = ['watcher', 'time table', 'friday']
    > array[1].artist = 'beatles'
    > array[1].album = 'abbey road'
    > array[1].songs = ['come', 'something', 'maxwell']
    >
    > Everything I try generates errors or results in array[0].songs equaling
    > array[1].songs. I feel I'm missing something obvious.


    Maybe something like this?

    class music(object):
    def __init__(self):
    self.lst = {}
    def __setattr__(self, name, value):
    self.__dict__[name] = value

    array = []
    array.append(music())
    array.append(music())

    # begin quoting your code
    array[0].artist = 'genesis'
    array[0].album = 'foxtrot'
    array[0].songs = ['watcher', 'time table', 'friday']
    array[1].artist = 'beatles'
    array[1].album = 'abbey road'
    array[1].songs = ['come', 'something', 'maxwell']
    # end quoting your code

    print array[0].artist
    print array[1].artist
    print array[0].songs
    print array[1].songs


    --
    Gustavo Picon (http://tabo.aurealsys.com/)
    Aureal Systems S.A.C. (http://www.aureal.com.pe/)

    Tlf: (511) 243-0131
    Nextel: 9824*4625

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.2 (FreeBSD)

    iD8DBQBDLO5kywoV1jM0SwMRAmEyAJsGcTJjp3G79aMXnsQeVPgnt8pT9ACeNq7j
    y4Zza8B8LqDDogtlaJDfGXc=
    =LSy4
    -----END PGP SIGNATURE-----
    Gustavo Picon, Sep 18, 2005
    #3
  4. On Sat, 2005-09-17 at 23:34 -0500, Gustavo Picon wrote:
    > Maybe something like this?
    >
    > class music(object):
    > def __init__(self):
    > self.lst = {}
    > def __setattr__(self, name, value):
    > self.__dict__[name] = value
    >
    > array = []
    > array.append(music())
    > array.append(music())
    >
    > # begin quoting your code
    > array[0].artist = 'genesis'
    > array[0].album = 'foxtrot'
    > array[0].songs = ['watcher', 'time table', 'friday']
    > array[1].artist = 'beatles'
    > array[1].album = 'abbey road'
    > array[1].songs = ['come', 'something', 'maxwell']
    > # end quoting your code
    >
    > print array[0].artist
    > print array[1].artist
    > print array[0].songs
    > print array[1].songs
    >


    Actually, forget about that music class, all you need in that example
    is:

    class music:
    pass

    --
    Gustavo

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.2 (FreeBSD)

    iD8DBQBDLPA2ywoV1jM0SwMRAlYOAJ49R6AH4C7VOmvUJ5C+a9UPUsi1jACfbB2E
    nXzsyYTaBvOkYErSxNmT3R8=
    =FjDK
    -----END PGP SIGNATURE-----
    Gustavo Picon, Sep 18, 2005
    #4
  5. richard

    Guest

    That is a great example Gustavo...

    One way that Richard's error of array[0] equaling array[1] could be
    introduced would be by accidentally appending the 'music' class object
    onto his list, rather than creating a new instance of music each time.
    Changing the code:

    array.append(music())
    array.append(music())

    to:

    array.append(music)
    array.append(music)

    would produce the symptom described by Richard, as both array[0] and
    array[1] would be references for the music class object.

    ML
    , Sep 18, 2005
    #5
  6. On Sat, 17 Sep 2005 20:49:32 -0500, richard wrote:

    > I'd like to have an array in which the elements are various data types.
    > How do I do this in Python?
    >
    > For example:
    >
    > array[0].artist = 'genesis'
    > array[0].album = 'foxtrot'
    > array[0].songs = ['watcher', 'time table', 'friday']
    > array[1].artist = 'beatles'
    > array[1].album = 'abbey road'
    > array[1].songs = ['come', 'something', 'maxwell']
    >
    > Everything I try generates errors or results in array[0].songs equaling
    > array[1].songs. I feel I'm missing something obvious.


    Would you like to tell us what you have already tried, or should we guess?

    To get good answers, it helps to ask good questions. What have you tried.
    What errors did you generate? Most importantly, what are you hoping to do
    with your data structure after you've got it?

    One hint is to split the problem into two halves, then solve each one. It
    looks to me like you are trying to store a list of albums. So half the
    problem is solved: the list of albums is just a list. Each item is an
    album. Now you just have to decide on how you store each album. Here is
    one solution:

    # Create a single album.
    album = {"artist": "beetles", "title": "abbey road", \
    "songlist" = ['come together', 'something', 'maxwell']}

    # Store it in the album list.
    albums.append(album)

    You can change an item like this:

    # Oops, wrong artist...
    albums[0]["artist"] = "beatles"
    albums[0]["songlist"].append("mean mr mustard")

    Does this solution work for you? If not, what does it not do that you need
    it to do?


    --
    Steven.
    Steven D'Aprano, Sep 18, 2005
    #6
  7. richard

    richard Guest

    Gustavo Picon <> wrote in
    news::

    > On Sat, 2005-09-17 at 23:34 -0500, Gustavo Picon wrote:
    >> Maybe something like this?
    >>
    >> class music(object):
    >> def __init__(self):
    >> self.lst = {}
    >> def __setattr__(self, name, value):
    >> self.__dict__[name] = value
    >>
    >> array = []
    >> array.append(music())
    >> array.append(music())
    >>
    >> # begin quoting your code
    >> array[0].artist = 'genesis'
    >> array[0].album = 'foxtrot'
    >> array[0].songs = ['watcher', 'time table', 'friday']
    >> array[1].artist = 'beatles'
    >> array[1].album = 'abbey road'
    >> array[1].songs = ['come', 'something', 'maxwell']
    >> # end quoting your code
    >>
    >> print array[0].artist
    >> print array[1].artist
    >> print array[0].songs
    >> print array[1].songs
    >>

    >
    > Actually, forget about that music class, all you need in that example
    > is:
    >
    > class music:
    > pass


    I like that. I ended up doing:

    array = []

    title = 't1'
    name = 'n1'
    songs = ['s1', 's2']
    array.append([title,name,songs])
    title = 't2'
    name = 'n2'
    songs = ['s3', 's4']
    array.append([title,name,songs])

    Thank you and the other posters for the help.

    Must learn not to think in c/c++. Python is much easier - no malloc's
    and pointers to fuss with :)
    richard, Sep 18, 2005
    #7
  8. richard

    richard Guest

    "" <> wrote in
    news::

    > At the same time, if array[0].songs equals array[1] songs, you are
    > probably initializing both array[0] and array[1] with the same object.
    > Since array[0] and array[1] both refer to the same object, a change to
    > one will be reflected in the other.
    >
    > I hope this is what you're looking for.
    >
    > Michael Loritsch


    I believe that was one of the things I was doing.

    thanks
    richard, Sep 18, 2005
    #8
    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.
Similar Threads
  1. Michael
    Replies:
    0
    Views:
    531
    Michael
    Sep 16, 2003
  2. Soren Kuula
    Replies:
    2
    Views:
    543
    Henry S. Thompson
    Dec 1, 2005
  3. ramu
    Replies:
    2
    Views:
    314
    rlblaster
    Feb 20, 2006
  4. Gary Roach
    Replies:
    0
    Views:
    103
    Gary Roach
    Sep 1, 2013
  5. Fábio Santos
    Replies:
    0
    Views:
    115
    Fábio Santos
    Sep 4, 2013
Loading...

Share This Page