A suggestion for a possible Python module

Andrew Dalke adalke at mindspring.com
Mon Mar 10 01:36:30 CET 2003


Matt Gerrans:
>    while x > 999:
>       chunks.append( x - (x / 1000) * 1000 )
>       x = x / 1000

See the divmod function for a way to make this easier

>>> divmod(12345, 1000)
(12, 345)
>>>

I would do it like this:

while x >= 1000:
  x, remainder = divmod(x, 1000)
  chunks.append(remainder)

The comparison to 1000 is mostly a preference, as I like
having 1000 used twice, rather than 1000 used once and
(1000-1) used in another place.

                    Andrew
                    dalke at dalkescientific.com






More information about the Python-list mailing list