splitting tables

robsom no.mail at no.mail.it
Tue Feb 10 19:44:18 EST 2004


Il Tue, 10 Feb 2004 18:09:33 +0100, Karl Pflästerer ha scritto:
> If you're interested there's a vrey good mailing list for Python
> starters (and others): Python Tutor (I don't know the correct address
> ath the momwnr but it should be easy to find on the ython web site).

I'll have a look, I'm beginning to appreciate Python even though at first
I hated this indentation thing and I (still) don't like the "for in
range()" way to build loops.


>> First question: shouldn't it be split(line)?
> 
> No.  The split method is a method of strings.  Since line is a string
> you call here a method of a string object.

Ok, but then why if I write this

for line in fin.readlines():
	line = split(line)

it does work anyway? Are there two ways to use the same function?
 
>> Now, my book says that lambda works by applying (in this example) the
>> instructions in "dat != '-9999' and dat or ''" to the dat variable. Does
>> this mean that (I'm more or less guessing now) this code splits the line
> 
> It gets splitted on spaces.
> 
>> assign each element to a dat variable which could have three types of
>> value (i.e. its "normal" value or -9999 or a space)? And then you use a
> 
> That's nearly right.  This trick with `and' and `or' is necessary since
> you can't have statements in Python lambda. Written the above as `if'
> statement would be:
> if dat != '9999':
>     return dat
> else:
>     return ''

So in the end here is ho this piece of code works:
takes a line, split the line on spaces and put each element into a dat
variable. if the dat variable is equal to '9999' or whatever other symbol
is used to indicate a blank substitute it with a space otherwise leave its
value. then put together the dat (into a list?) separated by a comma.
I got it! maybe...

> No since join is a method.  If you have an older Python book it may be
> written differently.

well I'm using "Learning to Program" by Alan Gauld which I found on the
python website. But I found the description of the join function on the
python documentation of my system. Anyway, as I said it works both ways
(at least for split, haven't tried with join).
I was thinking of buying "Learning Python" of the O'Reilly series. What do
you think?
 
> I didn't know you were a beginner otherwise I would have written more
> verbosely.  The Tutor list might be the right place for you to start.

don't worry I should have said :)
 
>> Morevoer I wonder if it is possible to achieve the same thing  without
>> using the functional programming.
> 
> Yes, but it would be less fun :-)

uhm... maybe... I'll tell you when I get there... :D:D
thanks a lot

R






More information about the Python-list mailing list