nested data structures in classes

Discussion in 'Python' started by Joel Forrest Moxley, Aug 24, 2003.

  1. 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
     
    Joel Forrest Moxley, Aug 24, 2003
    #1
    1. Advertising

  2. Joel Forrest Moxley

    Joel Guest

    Does anyone have any thoughts on this?

    Joel Forrest Moxley <> wrote in message news:<>...
    > 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
     
    Joel, Aug 29, 2003
    #2
    1. Advertising

  3. Joel Forrest Moxley

    Joel Guest

    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])



    (Joel) wrote in message news:<>...
    > Does anyone have any thoughts on this?
    >
    > Joel Forrest Moxley <> wrote in message news:<>...
    > > 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
     
    Joel, Sep 11, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joel
    Replies:
    1
    Views:
    320
    Tyler Eaves
    Aug 24, 2003
  2. Alfonso Morra
    Replies:
    11
    Views:
    754
    Emmanuel Delahaye
    Sep 24, 2005
  3. Clint Olsen

    Help with tied/nested data structures

    Clint Olsen, Jul 19, 2006, in forum: Perl Misc
    Replies:
    12
    Views:
    164
    Mumia W.
    Jul 20, 2006
  4. Sean Murphy
    Replies:
    0
    Views:
    95
    Sean Murphy
    Jan 7, 2014
  5. Sean Murphy
    Replies:
    1
    Views:
    101
    Steven D'Aprano
    Jan 7, 2014
Loading...

Share This Page