[Tutor] changing char list to int list isn't working

Alan Gauld alan.gauld at btinternet.com
Sat May 4 10:17:29 CEST 2013


On 04/05/13 05:13, Jim Mooney wrote:
> I'm turning an integer into a string so I can make a list of separate
> chars, then turn those chars back into individual ints,

You don't actually need to convert to chars, you could
use divmod to do it directly on the numbers:

 >>> digits = []
 >>> root = 455
 >>> while root > 0:
...     root, n = divmod(root,10)
...     digits.insert(0,n)
...
 >>> digits
[4, 5, 5]

But I suspect the str() method is slightly faster...

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list