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? :/ | | | | 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 | | | | 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 | | | | 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 | | | | 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! | | | | 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? | | | | 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 | | | | 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 | | | | 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) | | | | 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())
;-) | | | | 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! |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,546 network members.
|