complex data types?

R

richard

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.
 
R

rixil

richard said:
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
 
G

Gustavo Picon

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/)
(e-mail address removed)
Tlf: (511) 243-0131
Nextel: 9824*4625

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

iD8DBQBDLO5kywoV1jM0SwMRAmEyAJsGcTJjp3G79aMXnsQeVPgnt8pT9ACeNq7j
y4Zza8B8LqDDogtlaJDfGXc=
=LSy4
-----END PGP SIGNATURE-----
 
G

Gustavo Picon

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-----
 
R

rixil

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
 
S

Steven D'Aprano

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?
 
R

richard

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

richard

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,480
Members
44,900
Latest member
Nell636132

Latest Threads

Top