noob help request - how to make a list of defined class?

Discussion in 'Python' started by nuffnough@gmail.com, Sep 9, 2008.

  1. Guest

    I have defined two classes with one common field (called code) and
    several different fields.
    In class A there is only one instance of any given code as all items
    are individual.
    In class B, there may be none, one or many instances of each code, as
    there can be any number of Bs referring to a single A.

    I need to make a list of Bs, remove dupes, and then create a list of
    As that have Bs.

    (I hope this makes sense!)

    with much research and reading of my book I managed to get this
    working, but at one stage I had errors that talked about being unable
    to concatenate type A. So I converted my As to string, and then did a
    split on commas to make a list. This worked fine for all the As that
    didn't have a comma in a field (about 85%) but I can't help feeling
    that this is a kludge, and that if Icould find a way to get a proper
    list of As instead of a list of lists created by converting to string
    and splitting, then my app would work properly.

    So I am hoping some of the nice folk here will help me out.

    The relevent function looks like this:


    def gen_B_A_list():
    Bcodes = get_codes()
    all_As = read_A("A.csv")
    B_A = []
    for i in range(len(codes)):
    Bcode = Bcodes
    for A in all_As:
    filename = getattr(A, 'code')
    if filename == Bcode:
    BA = str(A)
    BA = string.split(BA, ',')
    B_A.append(BA)
    return B_A




    The element in question is after if filename == Bcode. How do I
    construct a list of my defined class A objects here instead of this?
    ( I can post the full code if needed, just thought it was better
    netiquette not to)

    TIA

    nuffi
     
    , Sep 9, 2008
    #1
    1. Advertising

  2. Jon Clements Guest

    On Sep 9, 3:11 pm, wrote:
    > I have defined two classes with one common field (called code) and
    > several different fields.
    > In class A there is only one instance of any given code as all items
    > are individual.
    > In class B, there may be none, one or many instances of each code, as
    > there can be any number of Bs referring to a single A.
    >
    > I need to make a list of Bs, remove dupes, and then create a list of
    > As that have Bs.
    >
    > (I hope this makes sense!)
    >
    > with much research and reading of my book I managed to get this
    > working,  but at one stage I had errors that talked about being unable
    > to concatenate type A.  So I converted my As to string, and then did a
    > split on commas to make a list.  This worked fine for all the As that
    > didn't have a comma in a field (about 85%) but I can't help feeling
    > that this is a kludge, and that if Icould find a way to get a proper
    > list of As instead of a list of lists created by converting to string
    > and splitting, then my app would work properly.
    >
    > So I am hoping some of the nice folk here will help me out.
    >
    > The relevent function looks like this:
    >
    > def gen_B_A_list():
    >     Bcodes = get_codes()
    >     all_As = read_A("A.csv")
    >     B_A = []
    >     for i in range(len(codes)):
    >         Bcode = Bcodes
    >         for A in all_As:
    >             filename = getattr(A, 'code')
    >             if filename == Bcode:
    >                 BA = str(A)
    >                 BA = string.split(BA, ',')
    >                 B_A.append(BA)
    >     return B_A
    >
    > The element in question is after if filename == Bcode.   How do I
    > construct a list of my defined class A objects here instead of this?
    > ( I can post the full code if needed,  just thought it was better
    > netiquette not to)
    >
    > TIA
    >
    > nuffi


    Looks like homework, but as you've made some kind of effort, some
    hints to where to refer to in the manual.

    1) Use the csv module for reading CSV files
    2) A dict maps one value to another... (check it out)
    3) Don't use getattr, use member access (the . symbol, eg: A.code)
    4) The for statement can loop over each item in term without an
    explicit range (eg, for bcode in codes...)
    5) Read the help for str, and read up on how to create objects, for
    instance str(A) returns A's string representation, while A() creates a
    new A object...

    Work your way through a python tutorial and make sure you understand
    the examples and principals... It might take you a while though, so I
    hope the assignment's not due soon!

    Jon.
     
    Jon Clements, Sep 9, 2008
    #2
    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. xchris
    Replies:
    5
    Views:
    4,137
  2. CJM
    Replies:
    7
    Views:
    1,865
    Steven Cheng[MSFT]
    Aug 13, 2004
  3. Oodini
    Replies:
    1
    Views:
    1,796
    Keith Thompson
    Sep 27, 2005
  4. Replies:
    8
    Views:
    397
    Oliver Wong
    Mar 21, 2007
  5. Replies:
    20
    Views:
    6,741
Loading...

Share This Page