[Tutor] map, filter and lambda functions
Sheila King
sheila@thinkspot.net
Wed, 22 Aug 2001 21:35:09 -0700
On Thu, 23 Aug 2001 00:27:17 -0400 (EDT), Ignacio Vazquez-Abrams
<ignacio@openservices.net> wrote about Re: [Tutor] map, filter and
lambda functions:
:On Wed, 22 Aug 2001, Sheila King wrote:
:> >>> mylist
:> ['a', 'b', 'c', '']
:> >>> mylist = map(string.upper(x), mylist)
:> Traceback (most recent call last):
:> File "<pyshell#6>", line 1, in ?
:> mylist = map(string.upper(x), mylist)
:> NameError: name 'x' is not defined
:> >>> mylist = map(string.upper(), mylist)
:> Traceback (most recent call last):
:> File "<pyshell#7>", line 1, in ?
:> mylist = map(string.upper(), mylist)
:> TypeError: upper() takes exactly 1 argument (0 given)
:> >>>
:>
:> If I do it with no argument, it complains that it requires an argument.
:> If I put an argument there, it complains that it has never heard of that
:> variable before.
:
:That's because the first argument to map has to be a function object; you'd
:have to use map(string.upper, mylist).
Ah, thanks. That's what I was missing. It certainly wasn't clear to me
from anything that I had read, that it was supposed to be a function
OBJECT as opposed to a function invocation.
For example, from the Python docs:
2.3 Built-in Functions
filter(function, list)
Construct a list from those elements of list for which function returns
true. If list is a string or a tuple, the result also has that type;
otherwise it is always a list. If function is None, the identity
function is assumed, i.e. all elements of list that are false (zero or
empty) are removed.
Maybe I should have picked up on that. But I certainly didn't.
--
Sheila King
http://www.thinkspot.net/sheila/
http://www.k12groups.org/