By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,630 Members | 1,313 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,630 IT Pros & Developers. It's quick & easy.

complex data types?

P: n/a
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.
Sep 18 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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 '05 #2

P: n/a
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/)
gp****@aureal.com.pe
Tlf: (511) 243-0131
Nextel: 9824*4625

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

iD8DBQBDLO5kywoV1jM0SwMRAmEyAJsGcTJjp3G79aMXnsQeVP gnt8pT9ACeNq7j
y4Zza8B8LqDDogtlaJDfGXc=
=LSy4
-----END PGP SIGNATURE-----

Sep 18 '05 #3

P: n/a
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+a9 UPUsi1jACfbB2E
nXzsyYTaBvOkYErSxNmT3R8=
=FjDK
-----END PGP SIGNATURE-----

Sep 18 '05 #4

P: n/a
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 '05 #5

P: n/a
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.

Sep 18 '05 #6

P: n/a
Gustavo Picon <gp****@aureal.com.pe> wrote in
news:ma************************************@python .org:
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 :-)
Sep 18 '05 #7

P: n/a
"ri***@hotmail.com" <ri***@hotmail.com> wrote in
news:11*********************@g43g2000cwa.googlegro ups.com:
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

Sep 18 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.