Conversion of string to integer

wittempj@hotmail.com martin.witte at gmail.com
Mon Jan 29 14:48:05 EST 2007



On Jan 29, 2:55 pm, "jupiter" <anil.jupit... at gmail.com> wrote:
> Hi guys,
>
> I have a problem. I have a list which contains strings and numeric.
> What I want is to compare them in loop, ignore string and create
> another list of numeric values.
>
> I tried int() and decimal() but without success.
>
> eq of problem is
>
> #hs=string.split(hs)
> hs =["popopopopop","254.25","pojdjdkjdhhjdccc","25452.25"]
>
> j=0
>     for o in range(len(hs)):
>         print hs[o],o
>         p=Decimal(hs[o])
>         if p > 200: j+=j
>         print "-"*5,j
>     print "*"*25
>
> I could be the best way to solve this ......?
>
> @nil

function isinstance can help you to determine the type/class of an 
object:

py>hs =["popopopopop","254.25","pojdjdkjdhhjdccc","25452.25"]
py>
py>for i in hs:
py>    if isinstance(i, str):
py>        print str(i)
py>    elif isinstance(i, float):
py>        print float(i)
py>    elif isinstance(i, int):
py>        print int(i)
py>    else:
py>        print 'dunno type of this element: %s' % str(i)
popopopopop
254.25
pojdjdkjdhhjdccc
25452.25




More information about the Python-list mailing list