complex data types?

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

    #1

    complex data types?

    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.
  • rixil@hotmail.com

    #2
    Re: complex data types?

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

    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

    Comment

    • Gustavo Picon

      #3
      Re: complex data types?

      On Sat, 2005-09-17 at 20:49 -0500, richard wrote:[color=blue]
      > 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.[/color]

      Maybe something like this?

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

      array = []
      array.append(mu sic())
      array.append(mu sic())

      # 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/)
      gpicon@aureal.c om.pe
      Tlf: (511) 243-0131
      Nextel: 9824*4625

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

      iD8DBQBDLO5kywo V1jM0SwMRAmEyAJ sGcTJjp3G79aMXn sQeVPgnt8pT9ACe Nq7j
      y4Zza8B8LqDDogt laJDfGXc=
      =LSy4
      -----END PGP SIGNATURE-----

      Comment

      • Gustavo Picon

        #4
        Re: complex data types?

        On Sat, 2005-09-17 at 23:34 -0500, Gustavo Picon wrote:[color=blue]
        > Maybe something like this?
        >
        > class music(object):
        > def __init__(self):
        > self.lst = {}
        > def __setattr__(sel f, name, value):
        > self.__dict__[name] = value
        >
        > array = []
        > array.append(mu sic())
        > array.append(mu sic())
        >
        > # 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
        > [/color]

        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)

        iD8DBQBDLPA2ywo V1jM0SwMRAlYOAJ 49R6AH4C7VOmvUJ 5C+a9UPUsi1jACf bB2E
        nXzsyYTaBvOkYEr SxNmT3R8=
        =FjDK
        -----END PGP SIGNATURE-----

        Comment

        • rixil@hotmail.com

          #5
          Re: complex data types?

          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(mu sic())
          array.append(mu sic())

          to:

          array.append(mu sic)
          array.append(mu sic)

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

          ML

          Comment

          • Steven D'Aprano

            #6
            Re: complex data types?

            On Sat, 17 Sep 2005 20:49:32 -0500, richard wrote:
            [color=blue]
            > 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.[/color]

            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(a lbum)

            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.

            Comment

            • richard

              #7
              Re: complex data types?

              Gustavo Picon <gpicon@aureal. com.pe> wrote in
              news:mailman.55 0.1127018555.50 9.python-list@python.org :
              [color=blue]
              > On Sat, 2005-09-17 at 23:34 -0500, Gustavo Picon wrote:[color=green]
              >> Maybe something like this?
              >>
              >> class music(object):
              >> def __init__(self):
              >> self.lst = {}
              >> def __setattr__(sel f, name, value):
              >> self.__dict__[name] = value
              >>
              >> array = []
              >> array.append(mu sic())
              >> array.append(mu sic())
              >>
              >> # 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
              >>[/color]
              >
              > Actually, forget about that music class, all you need in that example
              > is:
              >
              > class music:
              > pass[/color]

              I like that. I ended up doing:

              array = []

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

              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 :-)

              Comment

              • richard

                #8
                Re: complex data types?

                "rixil@hotmail. com" <rixil@hotmail. com> wrote in
                news:1127017357 .053187.45370@g 43g2000cwa.goog legroups.com:
                [color=blue]
                > 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[/color]

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

                thanks

                Comment

                Working...