Inserting '-' character in front of all numbers in a string

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

    Inserting '-' character in front of all numbers in a string

    Hey guys,

    I want to be able to insert a '-' character in front of all numeric
    values in a string. I want to insert the '-' character to use in
    conjunction with the getopt.getopt() function.

    Rigt now, I'm implementing a menu system where users will be able to
    select a set of options like "2a 3ab" which corresponds to menu
    choices. However, with getopt.getopt() , it'll only return what I want
    if I input -2a -3ab as my string. I don't want the user have to insert
    a '-' character in front of all their choices, so I was thinking of
    accepting the string input first, then adding in the '-' character
    myself.

    So my qusetion is, how do I change:

    "2a 3ab" into "-2a -3ab".

    Regular expressions? :/

  • kyosohma@gmail.com

    #2
    Re: Inserting '-' character in front of all numbers in a string

    On Mar 30, 10:38 am, "kevinliu23 " <kevinli...@gma il.comwrote:
    Hey guys,
    >
    I want to be able to insert a '-' character in front of all numeric
    values in a string. I want to insert the '-' character to use in
    conjunction with the getopt.getopt() function.
    >
    Rigt now, I'm implementing a menu system where users will be able to
    select a set of options like "2a 3ab" which corresponds to menu
    choices. However, with getopt.getopt() , it'll only return what I want
    if I input -2a -3ab as my string. I don't want the user have to insert
    a '-' character in front of all their choices, so I was thinking of
    accepting the string input first, then adding in the '-' character
    myself.
    >
    So my qusetion is, how do I change:
    >
    "2a 3ab" into "-2a -3ab".
    >
    Regular expressions? :/
    Regular expressions would definitely work. Here's a hack though:

    tempInput = '2a 3ab'
    tempLst = tempInput.split (' ')

    output = ''
    for i in tempLst:
    output += ('-' + i + ' ')


    I'm sure there are many better and more elegant hacks than this.

    Mike

    Comment

    • Larry Bates

      #3
      Re: Inserting '-' character in front of all numbers in a string

      kevinliu23 wrote:
      Hey guys,
      >
      I want to be able to insert a '-' character in front of all numeric
      values in a string. I want to insert the '-' character to use in
      conjunction with the getopt.getopt() function.
      >
      Rigt now, I'm implementing a menu system where users will be able to
      select a set of options like "2a 3ab" which corresponds to menu
      choices. However, with getopt.getopt() , it'll only return what I want
      if I input -2a -3ab as my string. I don't want the user have to insert
      a '-' character in front of all their choices, so I was thinking of
      accepting the string input first, then adding in the '-' character
      myself.
      >
      So my qusetion is, how do I change:
      >
      "2a 3ab" into "-2a -3ab".
      >
      Regular expressions? :/
      >
      s="2a 3b"
      s="-%s -%s"% tuple(s.split() )

      -Larry

      Comment

      • Marc 'BlackJack' Rintsch

        #4
        Re: Inserting '-' character in front of all numbers in a string

        In <1175269107.769 945.241500@r56g 2000hsd.googleg roups.com>, kevinliu23
        wrote:
        "2a 3ab" into "-2a -3ab".
        In [8]: '-' + ' -'.join('2a 3ab 4xy'.split())
        Out[8]: '-2a -3ab -4xy'

        Ciao,
        Marc 'BlackJack' Rintsch

        Comment

        • kevinliu23

          #5
          Re: Inserting '-' character in front of all numbers in a string

          Hey guys, thanks for the quick replies. I'm looking for something more
          generic than adding it to "2a 3ab". For example, under the menu option
          2, there can be upwards of 8 other suboptions. I'll see what's
          suggested here and post back if I run into more problems. Thanks guys!

          Comment

          • Michael Bentley

            #6
            Re: Inserting '-' character in front of all numbers in a string


            On Mar 30, 2007, at 10:38 AM, kevinliu23 wrote:
            I want to be able to insert a '-' character in front of all numeric
            values in a string. I want to insert the '-' character to use in
            conjunction with the getopt.getopt() function.
            >
            Rigt now, I'm implementing a menu system where users will be able to
            select a set of options like "2a 3ab" which corresponds to menu
            choices. However, with getopt.getopt() , it'll only return what I want
            if I input -2a -3ab as my string. I don't want the user have to insert
            a '-' character in front of all their choices, so I was thinking of
            accepting the string input first, then adding in the '-' character
            myself.
            >
            So my qusetion is, how do I change:
            >
            "2a 3ab" into "-2a -3ab".
            >
            Will the first character always be a digit?

            for i in yourstring.spli t():
            if i[0].isdigit():
            yourstring = yourstring.repl ace(i, '-%s' % (i,))

            Or are these hex numbers?

            Comment

            • Paul McGuire

              #7
              Re: Inserting '-' character in front of all numbers in a string

              On Mar 30, 2:09 pm, Michael Bentley <mich...@jedimi ndworks.comwrot e:
              On Mar 30, 2007, at 10:38 AM, kevinliu23 wrote:
              >
              >
              >
              >
              >
              I want to be able to insert a '-' character in front of all numeric
              values in a string. I want to insert the '-' character to use in
              conjunction with the getopt.getopt() function.
              >
              Rigt now, I'm implementing a menu system where users will be able to
              select a set of options like "2a 3ab" which corresponds to menu
              choices. However, with getopt.getopt() , it'll only return what I want
              if I input -2a -3ab as my string. I don't want the user have to insert
              a '-' character in front of all their choices, so I was thinking of
              accepting the string input first, then adding in the '-' character
              myself.
              >
              So my qusetion is, how do I change:
              >
              "2a 3ab" into "-2a -3ab".
              >
              Will the first character always be a digit?
              >
              for i in yourstring.spli t():
              if i[0].isdigit():
              yourstring = yourstring.repl ace(i, '-%s' % (i,))
              >
              Or are these hex numbers?- Hide quoted text -
              >
              - Show quoted text -
              Your replace strategy is risky. If:

              yourstring = "1ab 2bc 3de 11ab"

              it will convert to

              -1ab -2bc -3de 1-1ab

              -- Paul


              Comment

              • John Nagle

                #8
                Re: Inserting '-' character in front of all numbers in a string

                Paul McGuire wrote:
                On Mar 30, 2:09 pm, Michael Bentley <mich...@jedimi ndworks.comwrot e:
                >
                >>On Mar 30, 2007, at 10:38 AM, kevinliu23 wrote:
                >>
                >>
                >>
                >>
                >>
                >>
                >>>I want to be able to insert a '-' character in front of all numeric
                >>>values in a string. I want to insert the '-' character to use in
                >>>conjunctio n with the getopt.getopt() function.
                ' '.join(map(lamb da x: '-' + x, s.split()))

                assuming that you just want to put a "-" in front of each field,
                regardless of its content.

                John Nagle

                Comment

                • Michael Bentley

                  #9
                  Re: Inserting '-' character in front of all numbers in a string


                  On Mar 30, 2007, at 4:41 PM, Paul McGuire wrote:
                  On Mar 30, 2:09 pm, Michael Bentley <mich...@jedimi ndworks.comwrot e:
                  >On Mar 30, 2007, at 10:38 AM, kevinliu23 wrote:
                  >>
                  >>
                  >>
                  >>
                  >>
                  >>I want to be able to insert a '-' character in front of all numeric
                  >>values in a string. I want to insert the '-' character to use in
                  >>conjunction with the getopt.getopt() function.
                  >>
                  >>Rigt now, I'm implementing a menu system where users will be able to
                  >>select a set of options like "2a 3ab" which corresponds to menu
                  >>choices. However, with getopt.getopt() , it'll only return what I
                  >>want
                  >>if I input -2a -3ab as my string. I don't want the user have to
                  >>insert
                  >>a '-' character in front of all their choices, so I was thinking of
                  >>accepting the string input first, then adding in the '-' character
                  >>myself.
                  >>
                  >>So my qusetion is, how do I change:
                  >>
                  >>"2a 3ab" into "-2a -3ab".
                  >>
                  >Will the first character always be a digit?
                  >>
                  >for i in yourstring.spli t():
                  > if i[0].isdigit():
                  > yourstring = yourstring.repl ace(i, '-%s' % (i,))
                  >>
                  >Or are these hex numbers?- Hide quoted text -
                  >>
                  >- Show quoted text -
                  >
                  Your replace strategy is risky. If:
                  >
                  yourstring = "1ab 2bc 3de 11ab"
                  >
                  it will convert to
                  >
                  -1ab -2bc -3de 1-1ab
                  True enough! Good catch! How's this?

                  for i in yourstring.spli t():
                  if i[0].isdigit():
                  yourstring = yourstring.repl ace(i, '-%s' % (i,), 1)


                  Comment

                  • Michael Bentley

                    #10
                    Re: Inserting '-' character in front of all numbers in a string


                    On Mar 30, 2007, at 5:42 PM, Michael Bentley wrote:
                    for i in yourstring.spli t():
                    if i[0].isdigit():
                    yourstring = yourstring.repl ace(i, '-%s' % (i,), 1)
                    *OR*

                    yourstring ' '.join(x[0].isdigit() and '-%s' % x or x for x in
                    yourstring.spli t())

                    ;-)


                    Comment

                    • Jorgen Grahn

                      #11
                      Re: Inserting '-' character in front of all numbers in a string

                      On 30 Mar 2007 08:38:27 -0700, kevinliu23 <kevinliu23@gma il.comwrote:
                      Hey guys,
                      >
                      I want to be able to insert a '-' character in front of all numeric
                      values in a string. I want to insert the '-' character to use in
                      conjunction with the getopt.getopt() function.
                      ....
                      "2a 3ab" into "-2a -3ab".
                      Are you sure you want getopt, or are you just reusing it because you
                      don't know enough about string parsing and REs?

                      -2a -3ab is a bit limited: if you run out of digits and have to use
                      10, 11, ... then getopt will treat '-10ab' as '-1' without argument
                      and -0 with 'ab' as argument. It will probably choke on the
                      argumentless -1, too.

                      /Jorgen

                      --
                      // Jorgen Grahn <grahn@ Ph'nglui mglw'nafh Cthulhu
                      \X/ snipabacken.dyn dns.org R'lyeh wgah'nagl fhtagn!

                      Comment

                      Working...