473,387 Members | 1,283 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

nested data structures in classes

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[i] => "list of reactants for reaction i"
where: nested_list_of_reactants[i][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
Jul 18 '05 #1
2 2811
Does anyone have any thoughts on this?

Joel Forrest Moxley <jf******@MIT.EDU> wrote in message news:<ma**********************************@python. org>...
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[i] => "list of reactants for reaction i"
where: nested_list_of_reactants[i][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

Jul 18 '05 #2
For posterity, here's what ended up working. Many thanks to Tom Minka
for showing me this. I understood the class stuff in principle, but I
just couldn't implement correctly.

class ReactionClass:
def __init__(self,Name='',Reactant=[],Product=[]):
self.Name = Name
self.Reactant = Reactant
self.Product = Product
def __str__(self):
# how to print the object
return self.Name+': '+str(self.Reactant)+' ->
'+str(self.Product
)

Reaction = []
# two different ways of defining a reaction
Reaction.append(ReactionClass('Kinase',['GLUC', 'ATP'],['G6P',
'ADP']))
print(Reaction[0].Name)
print(Reaction[0])
Reaction.append(ReactionClass())
Reaction[1].Name = 'and so on'
Reaction[1].Reactant = ['one', 'two']
Reaction[1].Product = 'something else'
print(Reaction[1])
print(Reaction[1].Reactant[1])

jf******@mit.edu (Joel) wrote in message news:<e3**************************@posting.google. com>...
Does anyone have any thoughts on this?

Joel Forrest Moxley <jf******@MIT.EDU> wrote in message news:<ma**********************************@python. org>...
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[i] => "list of reactants for reaction i"
where: nested_list_of_reactants[i][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

Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Joel | last post by:
python-list@python.org 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. ...
9
by: OKB (not okblacke) | last post by:
For a variety of reasons, I'm interested in putting together some code that will allow me to created structures out of nested classes, something like: class class1: def methA(self): print...
3
by: Rubén Campos | last post by:
Organizing classes, types, structures, enums and whatever other entities into nested namespaces requires to include into every header and implementation file the complete path of namespaces. Let me...
11
by: Alfonso Morra | last post by:
Hi, I have the ff data types : typedef enum { VAL_LONG , VAL_DOUBLE , VAL_STRING , VAL_DATASET }ValueTypeEnum ;
2
by: Bob Day | last post by:
Using VS2003, VB.NET, MSDE... I am looking at a demo program that, to my surprise, has nested classes, such as the example below. I guess it surprised me becuase you cannot have nested subs,...
2
by: miked | last post by:
I am architecting in a read only class for use in mapping data to a business object. The object makes strong use of nested classes and their ability to access protected fields. The downside is...
0
by: Mathieu Cartoixa | last post by:
Hi, I have a simple 2-tiers (client+database) application with simple Domain Model objects The Data Access Layer is abstracted via Data Mappers which use Data Transfer Objects to communicate...
9
by: Bill Grigg | last post by:
All, Can anyone supply an example or reference to an example of using reflection to determine the data types and array lengths contained in a nested stucture in C#? Actually, it is a structure...
8
by: Sheldon | last post by:
Hi, Can anyone help with this problem with setting up nested structures and initializing them for use. I have created several structs and placed them in a super struct that I will then pass to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.