Connecting Tech Pros Worldwide Forums | Help | Site Map

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

kevinliu23
Guest
 
Posts: n/a
#1: Mar 30 '07
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
Guest
 
Posts: n/a
#2: Mar 30 '07

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


On Mar 30, 10:38 am, "kevinliu23" <kevinli...@gmail.comwrote:
Quote:
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

Larry Bates
Guest
 
Posts: n/a
#3: Mar 30 '07

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


kevinliu23 wrote:
Quote:
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
Marc 'BlackJack' Rintsch
Guest
 
Posts: n/a
#4: Mar 30 '07

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


In <1175269107.769945.241500@r56g2000hsd.googlegroups .com>, kevinliu23
wrote:
Quote:
"2a 3ab" into "-2a -3ab".
In [8]: '-' + ' -'.join('2a 3ab 4xy'.split())
Out[8]: '-2a -3ab -4xy'

Ciao,
Marc 'BlackJack' Rintsch
kevinliu23
Guest
 
Posts: n/a
#5: Mar 30 '07

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!

Michael Bentley
Guest
 
Posts: n/a
#6: Mar 30 '07

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



On Mar 30, 2007, at 10:38 AM, kevinliu23 wrote:
Quote:
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.split():
if i[0].isdigit():
yourstring = yourstring.replace(i, '-%s' % (i,))

Or are these hex numbers?

Paul McGuire
Guest
 
Posts: n/a
#7: Mar 30 '07

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


On Mar 30, 2:09 pm, Michael Bentley <mich...@jedimindworks.comwrote:
Quote:
On Mar 30, 2007, at 10:38 AM, kevinliu23 wrote:
>
>
>
>
>
Quote:
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.
>
Quote:
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.
>
Quote:
So my qusetion is, how do I change:
>
Quote:
"2a 3ab" into "-2a -3ab".
>
Will the first character always be a digit?
>
for i in yourstring.split():
if i[0].isdigit():
yourstring = yourstring.replace(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


John Nagle
Guest
 
Posts: n/a
#8: Mar 30 '07

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


Paul McGuire wrote:
Quote:
On Mar 30, 2:09 pm, Michael Bentley <mich...@jedimindworks.comwrote:
>
Quote:
>>On Mar 30, 2007, at 10:38 AM, kevinliu23 wrote:
>>
>>
>>
>>
>>
>>
Quote:
>>>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.
' '.join(map(lambda x: '-' + x, s.split()))

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

John Nagle
Michael Bentley
Guest
 
Posts: n/a
#9: Mar 30 '07

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



On Mar 30, 2007, at 4:41 PM, Paul McGuire wrote:
Quote:
On Mar 30, 2:09 pm, Michael Bentley <mich...@jedimindworks.comwrote:
Quote:
>On Mar 30, 2007, at 10:38 AM, kevinliu23 wrote:
>>
>>
>>
>>
>>
Quote:
>>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.
>>
Quote:
>>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.
>>
Quote:
>>So my qusetion is, how do I change:
>>
Quote:
>>"2a 3ab" into "-2a -3ab".
>>
>Will the first character always be a digit?
>>
>for i in yourstring.split():
> if i[0].isdigit():
> yourstring = yourstring.replace(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.split():
if i[0].isdigit():
yourstring = yourstring.replace(i, '-%s' % (i,), 1)


Michael Bentley
Guest
 
Posts: n/a
#10: Mar 31 '07

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



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

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

;-)


Jorgen Grahn
Guest
 
Posts: n/a
#11: Apr 3 '07

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


On 30 Mar 2007 08:38:27 -0700, kevinliu23 <kevinliu23@gmail.comwrote:
Quote:
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.
....
Quote:
"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.dyndns.org R'lyeh wgah'nagl fhtagn!
Closed Thread


Similar Python bytes