473,396 Members | 2,029 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Conversion of string to integer

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.2 5"]

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

Jan 29 '07 #1
5 1951
jupiter wrote:
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.2 5"]
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
Jan 29 '07 #2


On Jan 29, 1:55 pm, "jupiter" <anil.jupit...@gmail.comwrote:
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

Jan 29 '07 #3


On Jan 29, 2:55 pm, "jupiter" <anil.jupit...@gmail.comwrote:
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.2 5"]

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.2 5"]
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

Jan 29 '07 #4


On Jan 30, 6:48 am, "witte...@hotmail.com" <martin.wi...@gmail.com>
wrote:
On Jan 29, 2:55 pm, "jupiter" <anil.jupit...@gmail.comwrote:
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.2 5"]
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.2 5"]
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

Jan 29 '07 #5
jupiter 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.2 5"]
hs = [.............]

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

--
Thinker Li - th*****@branda.to th********@gmail.com
http://heaven.branda.to/~thinker/GinGin_CGI.py

Jan 30 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: ken | last post by:
I've been looking for a solution to a string to long conversion problem that I've run into >>> x = 'e10ea210' >>> print x e10ea210 >>> y=long(x) Traceback (most recent call last): File...
4
by: Jim Hubbard | last post by:
I have some C# code that is supposed to wrap the defrag APIs and I am trying to convert it to VB.Net (2003). But, I keep having problems. The C# code is relatively short, so I'll post it...
2
by: Thomas Matthews | last post by:
Hi, I'm working with Borland C++ Builder 6.2. My project uses the std::string class. However, Borland in its infinite wisdom has its own string class, AnsiString. To make my life easier, I...
1
by: heirou | last post by:
I'm a novice in this subject....I've made a database that requires a time conversion. For example, if local time is 1200, determine the time in Korea. I use two fields: a date field, and a time...
7
by: Alphonse Giambrone | last post by:
How can I convert a string to a different type based on another string or other variable? For instance, instead of Dim i as Integer i = ctype("1000", Integer) I would like to do
15
by: Peter Afonin | last post by:
Hello, I'm struggling with the string conversion to MD5 which I've never user before. I have a string that I need to encode which looks approximately like this: ...
5
by: Ian Rutherford | last post by:
Heya guys, It seems VB .net no longer supports the awesome ability of VB 6 to declare something as a string and specify how long the string would be all in one line: Public myString as String *...
12
by: Dennis | last post by:
I get the following results: Cint(&HC5798A2F) returns -981890513 as it should and Clng("&HFFFFFFFFC5798A2F") returns -981890513 as it should. However when using a string ...
0
by: Jm | last post by:
Hi All I recently got some help with code conversion that i used in vb6 to block a specific tcp port. The code runs under a timer and is run on a very low tick count (Meaning it happens...
1
by: =?Utf-8?B?TWlrZSBN?= | last post by:
Hi, My conversion seems from VB6 to VB.Net of an interface to an API seem to have gone OK, but when I look at the output from the Debug.Print("Active server is " & xmlServerActive) statement,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.