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

K

kevinliu23

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? :/
 
K

kyosohma

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
 
L

Larry Bates

kevinliu23 said:
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
 
M

Marc 'BlackJack' Rintsch

kevinliu23 said:
"2a 3ab" into "-2a -3ab".

In [8]: '-' + ' -'.join('2a 3ab 4xy'.split())
Out[8]: '-2a -3ab -4xy'

Ciao,
Marc 'BlackJack' Rintsch
 
K

kevinliu23

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!
 
M

Michael Bentley

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?
 
P

Paul McGuire

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?- 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
 
J

John Nagle

' '.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
 
M

Michael Bentley

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?- 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)
 
M

Michael Bentley

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())

;-)
 
J

Jorgen Grahn

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
 

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
474,436
Messages
2,571,696
Members
48,796
Latest member
Greg L.
Top