[Tutor] transforming an integer to a list of integers

Kent Johnson kent37 at tds.net
Tue Feb 3 17:06:00 CET 2009


On Tue, Feb 3, 2009 at 10:17 AM, H.G. le Roy <hgleroy at gmail.com> wrote:

> One step for my solution should be transforming this big integer to a list
> of integers. I did:
>
> import math
>
> bignr = 12345
> bignrs =[]
>

The suggestions with strings are fine, but you can simplify your
approach as well:

> for i in xrange(math.ceil(math.log10(bignr)):

No need for the complicated range expression, just use
while bignr > 0:

>   rem = bignr % 10
>   bignrs.append(rem)
>   bignr /= 10

Using the divmod() function you can compute rem and the new value for
bignr in one operation:
bignr, rem = divmod(bignr, 10)

Kent


More information about the Tutor mailing list