Help with reduce().
grayson-wilson at home.com
Tue Jul 10 03:29:53 CEST 2001
Oh wait nevermind i got it.
EricIDLE <grayson-wilson at home.com> wrote in message
news:eMs27.664121$166.13711545 at news1.rdc1.bc.home.com...
> im sorry i still dont understand reduce can you explain it in simpiler
> Steve Holden <sholden at holdenweb.com> wrote in message
> news:Sds27.29662$F%5.1909477 at e420r-atl2.usenetserver.com...
> > "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
> > > Way, "Teach yourself python in 24 hours" is an extremely poorly
> > 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
> > > in range N which would be 1, 2 and multiplys them together to get 2...
> > > fine im good ... now comes the tricky part!! what does reduce do? Does
> > > take the whole range and make it into a number (12345678910) then
> > 2
> > > (the result of the function mult) or what does it do.
> > >
> > The reduce() function takes a function as its first argument and a
> > as its second. There's also an optional third argument, which we can
> > 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
> > 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
> > > of. I would understand it if it was x + y but the qutation marks are
> > > throw me off what do they mean?
> > >
> > This is another case where the interactive interpreter is your friend.
> > 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,
> > 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
> > 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
> > When you give reduce() a thrid argument, it starts with that and the
> > element of the sequence rather than the first two elements of the
> > 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
> > 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,
> > could even abreviate it to:
> > " ".join(x)
> > which tells the string containing a single space to insert itself
> > 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
> > regards
> > Steve
> > --
> > http://www.holdenweb.com/
More information about the Python-list