Help with reduce().

Quinn Dunkan quinn at yak.ugcs.caltech.edu
Tue Jul 10 02:39:02 CEST 2001


On Mon, 09 Jul 2001 23:53:35 GMT, EricIDLE <grayson-wilson at home.com> wrote:
>Ok well I have ANOTHER problem with one of the examples in my book (By The
>Way, "Teach yourself python in 24 hours" is an extremely poorly written book
>and I do not reccomend it to any newbies who are just starting off)
>any ways the example is as follows.
>
>n = range(1,11)
>def mult(x,y):
>    return x * y
>f = reduce(mult,n)
>print f
>
>I get everything up to reduce. I relize mult() takes the first two numbers
>in range N which would be 1, 2 and multiplys them together to get 2... ok
>fine im good ... now comes the tricky part!! what does reduce do? Does it
>take the whole range and make it into a number (12345678910) then subtract 2
>(the result of the function mult) or what does it do.

Try the reduce documentation?  At the prompt type 'print reduce.__doc__'.

Just to convince you how much better off you'd be reading the python
documentation, I'll explain it myself:

In other languages, 'reduce' is called 'fold' which is because you can think
of it as folding a function into a list.  Given the list [1,2,3] and the
function (+), you get 1 + 2 + 3.  You can fold from the right or left, but
don't worry about that now (in an eager language like python it doesn't make
any difference anyway).

Here's a possible definition of a simplified right reduce in python:

def reduce(f, lst):
    if len(lst) == 0:
        raise TypeError, 'reduce() of empty sequence with no initial value'
    elif len(lst) == 1:
        return lst[0]
    else:
        return f(lst[0], reduce(f, lst[1:]))

It's trying to emulate a functional idiom, so it's somewhat awkward and slow
in python.  A more natural python solution would be iterative but less clear.

>And i have another question (not related to first question)
>
>x = ['Thu', 'Aug', '5', '06:06:27', 'MDT', '1999']
>def strp(x,y):
> return x + ' ' + y
>r = reduce(strp,x)
>print r
>
>my problem is this line return "x + ' ' + y" what does it return the value
>of. I would understand it if it was x + y but the qutation marks are what
>throw me off what do they mean?

Quotes surround strings.  ' ' is a string containing a space.  strp sticks
together two strings with a space in between.  The above is an awkward, slow,
and hard to read way to write "string.join(x)".

If you're still at the point where you're not clear on string literals, and
this book is heaving all these higher-order functions at you, I'd agree it's
not very good (or you skipped a few chapters).  reduce, map, and filter are
meant to be a small convenience for people who either know what they're doing
or who just arrived from scheme or haskell or ml and want their old goggles
back.

You shouldn't be learning python with these functions, you should be getting
friendly with for loops and lists.  And dictionaries.  When you're comfortably
slinging those around, you can revisit reduce.  map and filter have been
somewhat eclipsed by list comprehensions.


Ignore the book.  Read the python tutorials on the net.  deja c.l.py for URLs.
Subscribe to the python-tutor list.  And when you want to know what something
does, try it at the prompt.



More information about the Python-list mailing list