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

Marcin ‘Qrczak’ Kowalczyk qrczak at knm.org.pl
Thu Apr 24 10:27:09 CEST 2008


Dnia 24-04-2008, czw o godzinie 07:40 +0200, "Martin v. Löwis" pisze:

> In this case, I wouldn't use a loop at all:
> 
> py> time=1901248
> py> minutes,seconds = divmod(time, 60)
> py> hours,minutes = divmod(minutes, 60)
> py> days,hours = divmod(hours,24)
> py> seconds,minutes,hours,days
> (28, 7, 0, 22)

divmod could be extended to more arguments:

days, hours, minutes, seconds = divmod(time, 24, 60, 60)

def divmod(x, d, *ds):
   if ds:
      q, *rs = divmod(x, *ds)
      q1, r1 = divmod2(q, d)
      return q1, r1, *rs
   else:
      return divmod2(x, d)

-- 
   __("<         Marcin Kowalczyk
   \__/       qrczak at knm.org.pl
    ^^     http://qrnik.knm.org.pl/~qrczak/



More information about the Python-3000 mailing list