On Wed, Apr 23, 2008 at 10:06 PM, Nicholas T <[EMAIL PROTECTED]> wrote: ... > > > > >>> time=1901248 > > > > >>> reduce(lambda a, b: a[:-1] + [a[-1]%b, math.floor(a[-1]/b)], > [[time], > > > > 60, 60, 24]) > > > > [28, 7.0, 0.0, 22.0] # secs, mins, hrs, days ... > It wasn't only posted to be cryptic, it's one thing that's difficult to > write with a for loop without a lot of verbosity (at least I couldn't figure > out how to do it...).
def dhms1(t): r = [] for d in 60, 60, 24: r.append(t % d) t //= d r.append(t) return r >>> dhms1(1901248) [28, 7, 0, 22] vs import math def dhms2(t): return reduce(lambda a, b: a[:-1] + [a[-1]%b, math.floor(a[-1]/b)], [[t], 60, 60, 24]) >>> dhms2(1901248) [28, 7.0, 0.0, 22.0] Not sure why you consider it advantageous to get a mixed list with the first item guaranteed to be an int vs the other three guaranteed to be floats with a 0 fractional part; the simple approach gives a list of four ints, which seems a much saner idea to me. Apart from that, putting each snippet into a string (resp. x for dhms1, y for dhms2) I see: >>> len(x) 119 >>> len(y) 117 Is 119 vs 117 characters "a LOT of verbosity"...?! OK, then what about...: >>> import re >>> len(re.sub(r'\s','',x)) 67 >>> len(re.sub(r'\s','',y)) 97 The conceptually simple approach is WAY SHORTER if you only count non-space characters (is whitespace "verbosity"...?!). If you're really SO keen to minimize the count of characters, I suspect Perl is much closer to your tastes than Python is (or, God willing, will EVER be). Alex _______________________________________________ Python-3000 mailing list Python-3000@python.org http://mail.python.org/mailman/listinfo/python-3000 Unsubscribe: http://mail.python.org/mailman/options/python-3000/archive%40mail-archive.com