Conversion of string to integer

J

jupiter

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
 
W

Wojciech =?ISO-8859-2?Q?Mu=B3a?=

jupiter said:
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"]

tmp = []
for s in hs:
try:
tmp.append( float(s) )
except ValueError:
# function float raises VE, if string 's' doesn't
# represent a number
pass

# tmp contains a list of floats
 
A

Adam

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.


You can iterate over the list and use the type() function to work out
what each entry and choose what to do with it.

type() works like so:
###Code###
a = ["test", 1.25, "test2"]
if type(a[2]) == str:
print "a string"


a string###End Code###

Adam
 
W

wittempj

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
 
J

John Machin

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 ......?
@nilfunction 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

Call me crazy, but I can't understand what the above code is meant to
demonstrate.

(1) All of the elements in "hs" are *known* to be of type str. The
above code never reaches the first elif. The OP's problem appears to
be to distinguish which elements can be *converted* to a numeric type.

(2) if isinstance(i, some_type) is true, then (usually) some_type(i)
does exactly nothing that is useful

Awaiting enlightenment ...

John
 
T

Thinker

jupiter said:
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"]
hs = [.............]

import re
reo = re.compile(r'^[0-9]+(\.[0-9]+)?$')
result = [e for e in hs if reo.match(e)]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top