Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > complex data types?

Reply
Thread Tools

complex data types?

 
 
richard
Guest
Posts: n/a
 
      09-18-2005
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.
 
Reply With Quote
 
 
 
 
rixil@hotmail.com
Guest
Posts: n/a
 
      09-18-2005
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

 
Reply With Quote
 
 
 
 
Gustavo Picon
Guest
Posts: n/a
 
      09-18-2005
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/)
http://www.velocityreviews.com/forums/(E-Mail Removed)
Tlf: (511) 243-0131
Nextel: 9824*4625

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

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

 
Reply With Quote
 
Gustavo Picon
Guest
Posts: n/a
 
      09-18-2005
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-----

 
Reply With Quote
 
rixil@hotmail.com
Guest
Posts: n/a
 
      09-18-2005
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

 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      09-18-2005
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.

 
Reply With Quote
 
richard
Guest
Posts: n/a
 
      09-18-2005
Gustavo Picon <(E-Mail Removed)> wrote in
news:(E-Mail Removed):

> 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
 
Reply With Quote
 
richard
Guest
Posts: n/a
 
      09-18-2005
"(E-Mail Removed)" <(E-Mail Removed)> wrote in
news:(E-Mail Removed) 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How complex is complex? Kottiyath Python 22 03-28-2009 10:11 PM
Accessing complex data from a linked list based data structure.. Ram C Programming 3 03-24-2009 02:15 AM
wsdl2java: method parameter a complex type that extends another complex type Robert Mark Bram Java 0 02-04-2007 10:06 AM
[XML Schema] Content type of complex type definition with complex content Stanimir Stamenkov XML 2 10-25-2005 10:16 AM
For expert on complex loops (reposted) - complex looping problem news.amnet.net.au Java 1 04-13-2004 07:10 AM



Advertisments