ï
I have writen a code to do the process you need.  And It is not only suitable for integers, but also decimals and negatives!  The main method is type conversion functions of Python as float(), str() and so on.  I wish it will give you some illumination.
    Juan Shen
 
#!/usr/local/bin/python
#Filename: float2list.py
 
import sys
 
try:
    a=float(raw_input('Input a number: '))
except ValueError:
    print 'Input is not a number.'
    sys.exit()
print 'The input number is:',a
   
#knock down number into list
lista=list(str(a))
print 'A number as a list:',lista
 
#Pack this number from list again
#Process negative number
if lista[0] is '-':
    sign=-1
    lista=lista[1:]
size1=lista.index('.')
size2=len(lista)-lista.index('.')-1
integer=sum([int(lista[i])*10**(size1-i-1) for i in range(0,size1)])
decimal=sum([float(lista[lista.index('.')+i])*10**(-i) for i in range(1,size2+1)])
a2=sign*(integer+decimal)
print 'The number is packed again:',a2
 
 
----- Original Message -----
From: kilovh
Sent: Tuesday, January 04, 2005 6:17 AM
Subject: [Tutor] Breaking down integers?

I would like to be able to take an integer, break it down into individual items in a list, and then put them back together. I know how to do this last part thanks to Orri Ganel and Guillermo Fernandex, but what about taking the integer apart?
 
Sorry if the questions have incredibly obvious answers, I'm new to this.
 
~kilovh
_______________________________________________
Tutor maillist  -  [email protected]
http://mail.python.org/mailman/listinfo/tutor

Reply via email to