Message from discussion
Newbie: Struggling again 'map'
Path: g2news1.google.com!news3.google.com!proxad.net!proxad.net!feeder1-1.proxad.net!club-internet.fr!feedme-small.clubint.net!news.astraweb.com!border1.a.newsrouter.astraweb.com!hwmnpeer01.ams!news.highwinds-media.com!kramikske.telenet-ops.be!nntp.telenet.be!kwabbernoot.telenet-ops.be!phobos.telenet-ops.be.POSTED!not-for-mail
From: Roel Schroeven <rschroev_nospam...@fastmail.fm>
User-Agent: Thunderbird 2.0.0.0 (Windows/20070326)
MIME-Version: 1.0
Newsgroups: comp.lang.python
Subject: Re: Newbie: Struggling again 'map'
References: <1180173252.906992.262570@q75g2000hsh.googlegroups.com>
In-Reply-To: <1180173252.906992.262570@q75g2000hsh.googlegroups.com>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Lines: 45
Message-ID: <O7V5i.198013$xN1.1347637@phobos.telenet-ops.be>
Date: Sat, 26 May 2007 11:47:58 GMT
NNTP-Posting-Host: 81.165.208.196
X-Complaints-To: abuse@telenet.be
X-Trace: phobos.telenet-ops.be 1180180078 81.165.208.196 (Sat, 26 May 2007 13:47:58 MEST)
NNTP-Posting-Date: Sat, 26 May 2007 13:47:58 MEST
Organization: Telenet Internet
mosscliffe schreef:
> for x,y in map("N/A", lista, listb): ########## Fails - Can not call a
> 'str'
> print "MAP:", x, "<<x y>>", y
>
> def fillwith(fillchars):
> return fillchars
>
> for x,y in map(fillwith("N/A"), lista, listb): ########## Fails also -
> Can not call a 'str'
> print "MAP:", x, "<<x y>>", y
The first argument to map is a function, which is called with the items
of the argument sequences. If the first argument is None, a default
function is used which returns a tuple of the items. In the case that
two input sequences are provided:
map(None, lista, listb)
is equivalent to:
def maketuple(a, b):
return a, b
map(maketuple, lista, listb)
So what you want to do can be done with map like this:
def make_fill_missing(fillchars):
def fill_missing(a, b):
if a is None:
a = fillchars
if b is None:
b = fillchars
return a, b
return fill_missing
map(make_fill_missing("N/A"), lista, listb))
--
If I have been able to see further, it was only because I stood
on the shoulders of giants. -- Isaac Newton
Roel Schroeven