Conversion of string to integer

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jupiter

    #1

    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.spli t(hs)
    hs =["popopopopop"," 254.25","pojdjd kjdhhjdccc","25 452.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

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

    #2
    Re: Conversion of string to integer

    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.spli t(hs)
    hs =["popopopopop"," 254.25","pojdjd kjdhhjdccc","25 452.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

    Comment

    • Adam

      #3
      Re: Conversion of string to integer



      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

      Comment

      • wittempj@hotmail.com

        #4
        Re: Conversion of string to integer



        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.spli t(hs)
        hs =["popopopopop"," 254.25","pojdjd kjdhhjdccc","25 452.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","pojdjd kjdhhjdccc","25 452.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
        pojdjdkjdhhjdcc c
        25452.25

        Comment

        • John Machin

          #5
          Re: Conversion of string to integer



          On Jan 30, 6:48 am, "witte...@hotma il.com" <martin.wi...@g mail.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.spli t(hs)
          hs =["popopopopop"," 254.25","pojdjd kjdhhjdccc","25 452.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","pojdjd kjdhhjdccc","25 452.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
          pojdjdkjdhhjdcc c
          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

          Comment

          • Thinker

            #6
            Re: Conversion of string to integer

            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.spli t(hs)
            hs =["popopopopop"," 254.25","pojdjd kjdhhjdccc","25 452.25"]
            >
            hs = [.............]

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



            --
            Thinker Li - thinker@branda. to thinker.li@gmai l.com


            Comment

            Working...