my integer input is suppose 12345 , i want to split and put it into an array as 1,2,3,4,5 . How will i be able to do it?
+11
A:
return array as string
>>> list(str(12345))
['1', '2', '3', '4', '5']
return array as integer
>>> map(int,str(12345))
[1, 2, 3, 4, 5]
S.Mark
2009-12-15 11:10:17
+1 for map function
Ikke
2009-12-15 11:47:07
+1
A:
Strings are just as iterable as arrays, so just convert it to string:
str(12345)
unwind
2009-12-15 11:10:17
+5
A:
[int(i) for i in str(number)]
or, if do not want to use a list comprehension or you want to use a base different from 10
from __future__ import division # for compatibility of // between Python 2 and 3
def digits(number, base=10):
assert number >= 0
if number == 0:
return [0]
l = []
while number > 0:
l.append(number % base)
number = number // base
return l
nd
2009-12-15 11:19:36
+2
A:
like @nd says but using the built-in function of int to convert to a different base
>>> [ int(i,16) for i in '0123456789ABCDEF' ]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
>>> [int(i,2) for i in "100 010 110 111".split()]
[4, 2, 6, 7]
I don't know what is the final objective but take a look also inside the decimal module of python for doing stuff like
>>> Decimal('3.1415926535') + Decimal('2.7182818285')
Decimal('5.85987')
fabrizioM
2009-12-15 11:40:24