nested data structures with classes

J

Joel

[sent to (e-mail address removed) at Sun, 24 Aug 2003 13:31:58 but
didn't seem to show up here]

Greetings python-list!

The good news is that I've been having a blast with Python since early
Spring. I've had great success in both learning the language from
online / usegroup resources and implementing it in one of my projects.
However, I can't pretend to be an expert, and I do not have a strong
comp sci background.

This is a question that I've had for a while. Usually, I'll stumble
across the answer with repeated attempts and re-searching the groups,
but I didn't have luck with this one, even in a Dietel book I picked
up at the library. I'm assuming there's an obvious answer, so maybe
ya'll can save me some time.

My Python use involves organizing nested list information. In a
simplified example, you could imagine a list of reactants and products
in a chemical reaction. Thus,

len(nested_list_of_reactants) = len(nested_list_of_products) =>
"number of reactions"
where: nested_list_of_reactants => "list of reactants for reaction
i"
where: nested_list_of_reactants[j] => "reactant j of reaction i"

And so forth. Obviously, there are many attributes associated with a
specific reaction. Currently, I just pass these attribute lists
(nested and non-nested) into and out of functions. However, I'd like
to be able to create a class that would streamline this.

E.g., (this is a MATLAB structure whose qualities I'd like to
emulate):
reaction(27).name = 'fawlty towers'
reaction(27).reactant(2).name = 'john cleese'

Currently, I'd have a list and a nested list to take care of this...
reaction_name[27] = 'fawlty towers' and reactants[27][2] = 'john
cleese' if this makes sense.

Any thoughts or suggestions on this type of data structuring would be
greatly appreciated. Python love, Joel
 
T

Tyler Eaves

Maybe you could use dictionaries?

ie:

reaction['name'] = 'fawlty towers'
reaction['reactant'] = []
reaction['reactant'].append(anotherdict)

etc...
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top