Help with reduce().

Steve Holden sholden at holdenweb.com
Mon Jul 9 20:48:13 EDT 2001


"EricIDLE" <grayson-wilson at home.com> wrote in message
news:3ur27.663627$166.13698874 at news1.rdc1.bc.home.com...
> 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.
>
The reduce() function takes a function as its first argument and a sequence
as its second. There's also an optional third argument, which we can ignore
for now. The function is applied to the first two items of the sequence,
then to that result and the third item of the sequence, then to that result
and the fourth item, and so on until the sequence is exhausted. Here's a
loop that might make it a bit clearer:

>>> def mult(x, y):
...  return x * y
...
>>> for i in range(2, 11):
...  n = range(1, i)
...  print i, ":", reduce(mult, n)
...
2 : 1
3 : 2
4 : 6
5 : 24
6 : 120
7 : 720
8 : 5040
9 : 40320
10 : 362880

Each result extends the value by one multiplication (by one less than I
because of the way range works).

> 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?
>
This is another case where the interactive interpreter is your friend. Try
it and see...

>>> x = 'one'
>>> y = 'two'
>>> x + y
'onetwo'
>>> x + ' ' + y
'one two'

You can see that the second statement constructs a string from *three*
values: the string bound to x, the string consisting of a single space, and
the string bound to y.

So all your second example does is append al lthe strings together, with
spaces in between them. It takes 'Thu' and 'Aug" and puts them together with
a space in between them, that takes *that* result and '5' and puts them
together with a space between them, and ... you probably see the point now.

When you give reduce() a thrid argument, it starts with that and the first
element of the sequence rather than the first two elements of the sequence,
allowing you to put an initial value into the string of function
evaluations:

>>> x = ['Thu', 'Aug', '5', '06:06:27', 'MDT', '1999']
>>> def strp(x, y):
...  return x + ' ' + y
...
>>> print reduce(strp, x, "Date is:")
Date is: Thu Aug 5 06:06:27 MDT 1999

I hope the book also (though possibly later) points out that a much better
way to do the same thing would be

x = ['Thu', 'Aug', '5', '06:06:27', 'MDT', '1999']
r = string.join(x, " ")

which uses a function from the string module. And, using string methods, you
could even abreviate it to:

" ".join(x)

which tells the string containing a single space to insert itself between
the elements of the list, returning the resulting long string. But that
probably won't appear until you're about ten hours into the book! Good luck.

regards
 Steve
--
http://www.holdenweb.com/








More information about the Python-list mailing list