map, filter & reduce...

John Roth newsgroups at jhrothjr.com
Sun Nov 16 07:55:21 EST 2003


"Ben" <crescent_au at yahoo.com> wrote in message
news:d99e1341.0311160416.20f8cf18 at posting.google.com...
> Hi all,
>
> I'm trying to figure out how how complex map, filter and reduce work
> based on the following piece of code from
> http://www-106.ibm.com/developerworks/linux/library/l-prog.html :
>
> bigmuls = lambda xs,ys: filter(lambda (x,y):x*y > 25, combine(xs,ys))
> combine = lambda xs,ys: map(None, xs*len(ys), dupelms(ys,len(xs)))
> dupelms = lambda lst,n: reduce(lambda s,t:s+t, map(lambda l,n=n:
> [l]*n, lst))
> print bigmuls((1,2,3,4),(10,15,3,22))
>
> The solution generated by the above code is: [(3, 10), (4, 10), (2,
> 15), (3, 15), (4, 15), (2, 22), (3, 22), (4, 22)]
>
> I'm stuck on the second line in "map(None, xs*len(ys),
> dupelms(ys,len(xs))"... Can someone explain me how the map function
> evaluates??

In this case, map is (almost) a synonym for zip. It's going to
create a list where each entry is a two item list containing the
corresponding elements from each of the inputs. The first
input is, of course, the result of the multiply, and the second
is whatever the dupelms() call produced.

John Roth
>
> Thanks
> Ben






More information about the Python-list mailing list