[Tutor] reduce with comprehension
John Fouhy
john at fouhy.net
Thu Nov 24 23:28:33 CET 2005
On 25/11/05, Nick Lunt <nick at javacat.f2s.com> wrote:
> > -----Original Message-----
> > >>> def f(x, y):
> > ... return x*y
> > ...
> > >>> arr = range(1, 10) # Don't want to include 0!
> > >>> reduce(f, arr) # Our target
> > 362880
> > >>> tmp = [1]
> > >>> [f(x, y) for x in arr for y in [tmp[-1]] if tmp.append(f(x,
> > y)) or True][-1]
> > 362880
> > >>> print 'Magic!'
> > Magic!
>
> LOL I feel like I've accidentally signed up for the perl-tutor list ;)
Oh no --- I have become that which I hate!
I must quickly go and do twenty 'import this's as penance for my sins...
--
John.
More information about the Tutor
mailing list