[Tutor] Re: List Comprehensions again [an example with filter()]
Erik Price
erikprice@mac.com
Sun, 27 Jan 2002 22:50:03 -0500
Excellent! I see what you're saying. filter() basically lets you
perform a function on another function, and returns only the results
which are true for that second function. Just like the name: filter.
Filters out all results that are not true. Or something to that effect.
Alright, that makes sense, but I noticed some other things in your
response (sort of unrelated) that I'd like to ask for a bit of
clarification on:
On Friday, January 25, 2002, at 10:19 PM, Danny Yoo wrote:
> ###
> for w in words:
> if w[-1] == 's':
> print w
> ###
Is using the three hashes a standard way of marking up example code in
Python? I see it a lot on this list, and was wondering if it was
specific to the language or just a convention of this list.
> ###
> def endsWithS(word):
> return word[-1] == 's'
>
> for w in words:
> if endsWithS(w):
> print w
> ###
This is something I haven't seen before -- using an expression as the
return value of a function. I take it that this means "return boolean
value TRUE if the expression 'word[-1] == "s" ' ". But I'm not sure.
It could be that instead, it returns the words that match? If it did
that, then the second part, the "for" loop, wouldn't be necessary, so
drawing from context I am assuming that the former is true.
> You probably already feel comfortable passing around numbers and strings
> to functions, but other functions? If this is the first time you've
> seen
> it, it will probably seem pretty darn weird at first, but it'll make
> sense
> as you play with it.
You can create some pretty complicated constructs by passing functions
to other functions in PHP -- I find that it's sometimes easier to
conceptualize if I assign a function to a variable and then just pass
variables to functions. But over time I'm sure I'll trade in this
clarity for convenience.
> On Thu, 24 Jan 2002, Erik Price wrote:
>> As a superfledgling, I have to say that I have no idea what you guys
>> are
>> talking about. For the first few messages I figured it was something
>> that I could pick up later on. Now this thread has gone on for several
>> days and I'm starting to wonder if I should learn this lambda / list
>
> You can pick it up later. Basically, list comprehensions, maps, and
> filters are fancy ways of doing list processing. Often, we're looking
> at
> a list --- a sequence of things --- and we might want to do some mass
> action to that whole sequence. Comprehensions, map(), and filter() are
> all convenient ways of doing operations on whole sequences.
Okay, I'll learn about list comprehensions later.
Thanks Danny!
Erik