splitting tables

Karl Pflästerer sigurd at 12move.de
Tue Feb 10 12:09:33 EST 2004


On 10 Feb 2004, robsom <- no.mail at no.mail.it wrote:

> Sorry guys, I thank all of you, but I'm a beginner and my knowledge of
> Python is not yet enough to follow you.

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

>>>>> for line in s:
>> ...print ','.join(map(lambda dat: dat != '-9999' and dat or '',
>> line.split())) ...

> As far as I understand you are using a 'map' function to apply the
> function "lambda dat: dat != '-9999' and dat or ''" to the elements
> obtained with "line.split()".

Right.

> 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.

> 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 ''

> join functions to put again together the elements yolu have separated with

`join' is here again (like `split') a method of the string object.

> map with a comma as separator? And here I have a second question:
> shouldn't it be "join(map(....),',')" instead of "','.join(map(...))"?

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

[...]
> Ok, but before going on, I'd like to know if I understand the code.
> Probably I got it all wrong, but this is quite a few chapters beyond where
> I am now in my book :)

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.

> Morevoer I wonder if it is possible to achieve the same thing  without
> using the functional programming.

Yes, but it would be less fun :-)

   KP

-- 
You know you've been sitting in front of your Lisp machine too long
when you go out to the junk food machine and start wondering how to
make it give you the CADR of Item H so you can get that yummie
chocolate cupcake that's stuck behind the disgusting vanilla one.



More information about the Python-list mailing list