[Python-3000] what do I use in place of reduce?

Nicholas T ntung at ntung.com
Thu Apr 24 19:53:19 CEST 2008


On Wed, Apr 23, 2008 at 10:35 PM, Alex Martelli <aleaxit at gmail.com> wrote:

> Is 119 vs 117 characters "a LOT of verbosity"...?!  OK, then what about...:


the reduce is actually 69 *and only one line* if you don't need it in a
function. You can't put the dhms2 in a function unless you want to leak a
bunch of variables like 'd', 'r', and 't'...
reduce(lambda a, b: a[:-1] + [a[-1]%b, a[-1]//b], [[t], 60, 60, 24])

On Wed, Apr 23, 2008 at 10:40 PM, "Martin v. Löwis" <martin at v.loewis.de>
wrote:

> py> time % 60, time//60%60, time//3600%24, time//(3600*24)
> (28, 7, 0, 22)

the 3600 and 3600*24 was what I was trying to avoid. I like the divmod
solution. You can also use it in the reduce :)
reduce(lambda a, b: divmod(a[0], b) + a[1:], [(t,), 60, 60, 24])[::-1]

P.S. I'm not sure why you had been using floating point
> operations.

no, this wasn't necessary, I didn't know about "//". I could have used int
as well...

Nicholas
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-3000/attachments/20080424/0f2dc9a8/attachment-0001.htm>


More information about the Python-3000 mailing list