Newbie: Struggling again 'map'

mosscliffe mcl.office at googlemail.com
Sat May 26 08:15:43 EDT 2007


Thank you all very much.

I particularily found Roel's explanation and example most useful.

At this stage I am getting my head around syntax, rather than language
theory, although I know, I have to understand that as well.

Thanks again.

Richard

On 26 May, 12:47, Roel Schroeven <rschroev_nospam... at fastmail.fm>
wrote:
> 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





More information about the Python-list mailing list