Stuck newbie

Discussion in 'Python' started by Mike Silva, Dec 2, 2003.

  1. Mike Silva

    Mike Silva Guest

    Hi all,

    I'm a Python newbie trying to write a simple program to do some
    processing of a text file. So far I'm able to extract all the data I
    need from the file into a list (fileinput is very nice!). Here's my
    problem. I want the list ("namelist") to contain records, with each
    record consisting of a string ("name"), an integer ("flags"), and two
    variable-length lists of integers ("list1" and "list2"). I gather
    that the corresponding data object in Python would be a sequence of
    those four items, correct? So what I want is a list of sequences?

    If that is correct, what I don't understand is how to reference and
    modify elements of an individual object. For example, I want to do
    things like (in pseudocode):

    namelist.append( new_record )
    .....
    namelist[ index ].flags = 99
    namelist[ index ].list1.append( 100 )

    But I don't see how to reference the individual elements of a data
    object. Any tips greatly appreciated!

    Mike
     
    Mike Silva, Dec 2, 2003
    #1
    1. Advertising

  2. > If that is correct, what I don't understand is how to reference and
    > modify elements of an individual object. For example, I want to do
    > things like (in pseudocode):


    The nice thing is - your pseudocode is quite close python-code ;)

    > namelist.append( new_record )
    > ....
    > namelist[ index ].flags = 99
    > namelist[ index ].list1.append( 100 )
    >


    namelist = []
    new_record = [0, [], []]
    namelist.append(new_record)
    namelist[-1][0] = 99
    namelist[-1][1].append(100)

    However, if you prefer to access the records fields using names, you can go
    with a dicionary:

    new_record = {'flags': 0, 'list1':[], 'list2':[]}

    Then access is like this

    namelist[-1]['flags'] = 99
    namelist[-1]['list1'].append(100)

    The last approach would be to create a class new_record:

    class new_record:
    def __init__(self):
    self.flags = 0
    self.list1 = []
    self.list2 = []

    Then your code above should work.

    Diez
     
    Diez B. Roggisch, Dec 2, 2003
    #2
    1. Advertising

  3. Mike Silva

    r.e.s. Guest

    "Diez B. Roggisch" <> wrote ...

    > The last approach would be to create a class new_record:
    >
    > class new_record:
    > def __init__(self):
    > self.flags = 0
    > self.list1 = []
    > self.list2 = []


    [another beginner here]

    Suppose I do the above, followed by this:

    records = []
    rec = new_record()
    rec.flags = 1
    rec.list1 = [10]
    rec.list2 = ['a']
    records.append(rec)
    print records
    # [<Script1.new_record instance at 0x00BCBDA0>]
    print records[-1]
    # <Script1.new_record instance at 0x00BCBDA0>
    print records[-1].flags, records[-1].list1, records[-1].list2
    # 1 [10] ['a']

    Is there a simpler way to accomplish the last line
    (as I expected the simplest "print records" to do)?

    Thanks.
    --
    r.e.s.
     
    r.e.s., Dec 2, 2003
    #3
  4. > print records[-1].flags, records[-1].list1, records[-1].list2
    > # 1 [10] ['a']
    >
    > Is there a simpler way to accomplish the last line
    > (as I expected the simplest "print records" to do)?


    First, you could assign records[-1] to a variable, like this

    foo = records[-1]
    print foo.flags, foo.list1, foo.list2

    Another way would be to implement __repr__ on record:

    class record:
    .... # the old stuff
    __repr__(self):
    return repr((self.flags, self.list1, self.list2))

    Then

    print records[-1]
    The __repr__ method is automagically called when print is used on an object.

    Diez
     
    Diez B. Roggisch, Dec 2, 2003
    #4
  5. Mike Silva

    sdd Guest

    Diez B. Roggisch wrote:

    >... Lotsa good stuff ... Then
    >
    > print records[-1]
    > The __repr__ method is automagically called when print is used on an object.


    Well, it tries to call the __str__ method, which defaults to the
    __repr__ method if no __str__ is defined.

    pedantically yours,
    -Scott David Daniels
     
    sdd, Dec 3, 2003
    #5
    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. as mellow as a horse

    Newbie stuck posting to an ASP page

    as mellow as a horse, May 10, 2004, in forum: HTML
    Replies:
    6
    Views:
    442
    as mellow as a horse
    May 10, 2004
  2. Newbie - Stuck

    , Oct 8, 2006, in forum: Python
    Replies:
    8
    Views:
    366
    Theerasak Photha
    Oct 10, 2006
  3. Sheetal
    Replies:
    3
    Views:
    808
  4. Martin Robertson

    newbie stuck with multiple conditions

    Martin Robertson, Nov 12, 2007, in forum: Ruby
    Replies:
    3
    Views:
    97
    Jacob Basham
    Nov 12, 2007
  5. Ed

    newbie stuck - please help

    Ed, Jul 2, 2003, in forum: Javascript
    Replies:
    0
    Views:
    97
Loading...

Share This Page