Stuck newbie

M

Mike Silva

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
 
D

Diez B. Roggisch

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
 
R

r.e.s.

Diez B. Roggisch said:
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.
 
D

Diez B. Roggisch

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
 
S

sdd

Diez said:
... 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
(e-mail address removed)
 

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,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top