Re: map, filter & reduce...

From: John Roth (newsgroups_at_jhrothjr.com)
Date: 11/16/03


Date: Sun, 16 Nov 2003 07:55:21 -0500


"Ben" <crescent_au@yahoo.com> wrote in message
news:d99e1341.0311160416.20f8cf18@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



Relevant Pages

  • Re: Too much builtins (was Re: Pythons simplicity philosophy
    ... > I also think that reduce, sum, map and filter (and lots of others, ... There are no way in which LC's and genexp can "take care of" CONSUMING ...
    (comp.lang.python)
  • Re: Fate of lambda, Functional Programming in Python...
    ... Simple map and filter implementations ... if init is None: ... Macros for an infix language are not impossible, ...
    (comp.lang.python)
  • Re: "Collapsing" a list into a list of changes
    ... > filter(str.strip, lst) ... > Note that, unlike reduce, map and filter aren't really going to increase ... Consider the equivalent list comprehensions: ...
    (comp.lang.python)
  • Re: dbcopy over secure/encrypted line
    ... The onpladm command works great. ... create map ... describe format ... describe filter ...
    (comp.databases.informix)
  • Re: trying to match a string
    ... You didn't show the output from map() i.e. something like [None, ... read up on the filter and reduce functions. ... I put together a generic matcher that returns either a list of True data or a boolean value: ... "ex is the regular expression to match for, var the iterable or string to return a list of matching items or a boolean value respectively." ...
    (comp.lang.python)

Loading