Re: alternating string replace

2008-01-11 Thread cokofreedom
evenOrOdd = True s1, s2 = hi_cat_bye_dog_foo_bar_red, for i in s1: if i == '_': s2 += ':' if evenOrOdd else ',' evenOrOdd = not evenOrOdd else: s2 += i print s2 Presently I cannot work out how to use .join instead of += ... While I realise this is producing a new

Re: alternating string replace

2008-01-11 Thread Chris
On Jan 9, 12:34 pm, cesco [EMAIL PROTECTED] wrote: Hi, say I have a string like the following: s1 = 'hi_cat_bye_dog' and I want to replace the even '_' with ':' and the odd '_' with ',' so that I get a new string like the following: s2 = 'hi:cat,bye:dog' Is there a common recipe to

Re: alternating string replace

2008-01-11 Thread bearophileHUGS
Dennis Lee Bieber: So� in Python, your str[n] := ':' just can not be done! You would have to create a new string containing everything in front of n, the ':', and then everything behind n (skipping n itself, of course). This is a painfully slow operation in Python as it allocates memory for

Re: alternating string replace

2008-01-11 Thread Paddy
On Jan 11, 9:31 am, [EMAIL PROTECTED] wrote: evenOrOdd = True s1, s2 = hi_cat_bye_dog_foo_bar_red, for i in s1: if i == '_': s2 += ':' if evenOrOdd else ',' evenOrOdd = not evenOrOdd else: s2 += i print s2 Presently I cannot work out how to use .join

Re: alternating string replace

2008-01-11 Thread Paddy
On Jan 11, 9:54 am, Chris [EMAIL PROTECTED] wrote: On Jan 9, 12:34 pm, cesco [EMAIL PROTECTED] wrote: Hi, say I have a string like the following: s1 = 'hi_cat_bye_dog' and I want to replace the even '_' with ':' and the odd '_' with ',' so that I get a new string like the following:

RE: alternating string replace

2008-01-11 Thread Reedick, Andrew
-Original Message- From: [EMAIL PROTECTED] [mailto:python- [EMAIL PROTECTED] On Behalf Of cesco Sent: Wednesday, January 09, 2008 5:34 AM To: python-list@python.org Subject: alternating string replace Hi, say I have a string like the following: s1 = 'hi_cat_bye_dog' and I

Re: alternating string replace

2008-01-11 Thread George Sakkis
On Jan 9, 6:34 am, Duncan Booth [EMAIL PROTECTED] wrote: cesco [EMAIL PROTECTED] wrote: Hi, say I have a string like the following: s1 = 'hi_cat_bye_dog' and I want to replace the even '_' with ':' and the odd '_' with ',' so that I get a new string like the following: s2 =

Re: alternating string replace

2008-01-11 Thread Paul Rubin
George Sakkis [EMAIL PROTECTED] writes: from itertools import chain, izip, cycle print ''.join(chain(*izip(s1.split('_'),cycle(':,'[:-1] from itertools import cycle a = cycle(':,') print re.sub('_', lambda x: a.next(), s1) -- http://mail.python.org/mailman/listinfo/python-list

Re: alternating string replace

2008-01-11 Thread Steven D'Aprano
On Fri, 11 Jan 2008 14:55:18 -0600, Reedick, Andrew wrote: For those of us who still think in Perl, here's an easy to read ... s = re.sub(r'_(.*?(_|$))', r':\1', s) Easy to read? Oh that's priceless. Andrew, you should consider writing comedy professionally! -- Steven --

Re: alternating string replace

2008-01-11 Thread Pablo Ziliani
Paul Rubin wrote: George Sakkis [EMAIL PROTECTED] writes: from itertools import chain, izip, cycle print ''.join(chain(*izip(s1.split('_'),cycle(':,'[:-1] from itertools import cycle a = cycle(':,') print re.sub('_', lambda x: a.next(), s1) Lovely. If there OP didn't

Re: alternating string replace

2008-01-11 Thread Paddy
On Jan 12, 2:55 am, Pablo Ziliani [EMAIL PROTECTED] wrote: * die, thread! :-) def altrep7(s): from itertools import cycle import re a = cycle(':,') return re.sub('_', lambda x: a.next(), s) altrep7.author=George Sakkis(Paul Rubin) Gives: ## Program by: George Sakkis(Paul Rubin)

Re: alternating string replace

2008-01-11 Thread Paddy
On Jan 11, 8:55 pm, Reedick, Andrew [EMAIL PROTECTED] wrote: -Original Message- From: [EMAIL PROTECTED] [mailto:python- [EMAIL PROTECTED] On Behalf Of cesco Sent: Wednesday, January 09, 2008 5:34 AM To: [EMAIL PROTECTED] Subject: alternating string replace Hi, say I have

Re: alternating string replace

2008-01-10 Thread cokofreedom
On Jan 10, 3:46 am, [EMAIL PROTECTED] wrote: Gordon C: This is very cool stuff but I suspect that the code is unreadable to many readers, including me. Just for fun here is a complete program, written in Turbo Pascal, circa 1982, that does the job. Readable n'est pas? I think it's

alternating string replace

2008-01-09 Thread cesco
Hi, say I have a string like the following: s1 = 'hi_cat_bye_dog' and I want to replace the even '_' with ':' and the odd '_' with ',' so that I get a new string like the following: s2 = 'hi:cat,bye:dog' Is there a common recipe to accomplish that? I can't come up with any solution... Thanks in

Re: alternating string replace

2008-01-09 Thread Fredrik Lundh
cesco wrote: and I want to replace the even '_' with ':' and the odd '_' with ',' so that I get a new string like the following: s2 = 'hi:cat,bye:dog' Is there a common recipe to accomplish that? I can't come up with any solution... how about splitting on _, joining pairs with :, and

Re: alternating string replace

2008-01-09 Thread Peter Otten
cesco wrote: say I have a string like the following: s1 = 'hi_cat_bye_dog' and I want to replace the even '_' with ':' and the odd '_' with ',' so that I get a new string like the following: s2 = 'hi:cat,bye:dog' import re from itertools import cycle re.sub(_, lambda m, c=cycle(:,).next:

Re: alternating string replace

2008-01-09 Thread Duncan Booth
cesco [EMAIL PROTECTED] wrote: Hi, say I have a string like the following: s1 = 'hi_cat_bye_dog' and I want to replace the even '_' with ':' and the odd '_' with ',' so that I get a new string like the following: s2 = 'hi:cat,bye:dog' Is there a common recipe to accomplish that? I can't

Re: alternating string replace

2008-01-09 Thread bearophileHUGS
My version, uses a re.sub, plus a function used as an object with a one bit state: from re import sub def repl(o): repl.n = not repl.n return : if repl.n else , repl.n = False print sub(_, repl, hi_cat_bye_dog_foo_bar_red) Bye, bearophile --

Re: alternating string replace

2008-01-09 Thread cokofreedom
Designed a pretty basic way that is acceptable on small strings. evenOrOdd = True s1 = hi_cat_bye_dog_foo_bar_red s2 = for i in s1: if i == '_': if evenOrOdd: s2 += ':' evenOrOdd = not evenOrOdd else: s2 += ',' evenOrOdd = not

Re: alternating string replace

2008-01-09 Thread Neil Cerutti
On Jan 9, 2008 5:34 AM, cesco [EMAIL PROTECTED] wrote: Hi, say I have a string like the following: s1 = 'hi_cat_bye_dog' and I want to replace the even '_' with ':' and the odd '_' with ',' so that I get a new string like the following: s2 = 'hi:cat,bye:dog' Is there a common recipe to

Re: alternating string replace

2008-01-09 Thread Neil Cerutti
On Jan 9, 2008 5:34 AM, cesco [EMAIL PROTECTED] wrote: Hi, say I have a string like the following: s1 = 'hi_cat_bye_dog' and I want to replace the even '_' with ':' and the odd '_' with ',' so that I get a new string like the following: s2 = 'hi:cat,bye:dog' Is there a common recipe to

Re: alternating string replace

2008-01-09 Thread Pablo Ziliani
cesco wrote: Hi, say I have a string like the following: s1 = 'hi_cat_bye_dog' and I want to replace the even '_' with ':' and the odd '_' with ',' so that I get a new string like the following: s2 = 'hi:cat,bye:dog' Is there a common recipe to accomplish that? I can't come up with any

Re: alternating string replace: Extended input (Long).

2008-01-09 Thread Donald 'Paddy' McCarthy
cesco wrote: I created some more test strings and ran posters solutions against them. results attached. - Paddy. # alternating_replacements.py tests = 1 2_ 3_4 5_6_ 7_8_9 10_11_12_ 13_14_15_16 17_18_19_20_ \ _ _21 _22_ _23_24 _25_26_ _27_28_29 _30_31_32_ _33_34_35_36 \ __ ___

Re: alternating string replace

2008-01-09 Thread Paul McGuire
On Jan 9, 7:41 am, Neil Cerutti [EMAIL PROTECTED] wrote: On Jan 9, 2008 5:34 AM, cesco [EMAIL PROTECTED] wrote: Hi, say I have a string like the following: s1 = 'hi_cat_bye_dog' and I want to replace the even '_' with ':' and the odd '_' with ',' so that I get a new string like the

Re: alternating string replace: Extended input (Long).

2008-01-09 Thread bearophileHUGS
Donald 'Paddy' McCarthy: [... lots and lots and lots of tests...] C'mon Paddy, where are the timings too? Are you becoming lazy lately? ;-) Bear bugs, bearophile -- http://mail.python.org/mailman/listinfo/python-list

Re: alternating string replace: Extended input (Long).

2008-01-09 Thread Fredrik Lundh
Donald 'Paddy' McCarthy wrote: I created some more test strings and ran posters solutions against them. the point being? /F -- http://mail.python.org/mailman/listinfo/python-list

Re: alternating string replace: Extended input (Long).

2008-01-09 Thread Paddy
On Jan 9, 8:47 pm, [EMAIL PROTECTED] wrote: Donald 'Paddy' McCarthy: [... lots and lots and lots of tests...] C'mon Paddy, where are the timings too? Are you becoming lazy lately? ;-) Bear bugs, bearophile Get it right before you get it fast. But what is 'right'. --

Re: alternating string replace: Extended input (Long).

2008-01-09 Thread Paddy
On Jan 9, 8:56 pm, Fredrik Lundh [EMAIL PROTECTED] wrote: Donald 'Paddy' McCarthy wrote: I created some more test strings and ran posters solutions against them. the point being? /F To see how they act against 'corner cases' and an exercise for me in trying to create corner cases. (I'm in

Re: alternating string replace: Extended input (Long).

2008-01-09 Thread Paddy
On Jan 9, 9:29 pm, Paddy [EMAIL PROTECTED] wrote: On Jan 9, 8:56 pm, Fredrik Lundh [EMAIL PROTECTED] wrote: Donald 'Paddy' McCarthy wrote: I created some more test strings and ran posters solutions against them. the point being? /F To see how they act against 'corner cases' and an

Re: alternating string replace: Extended input (Long).

2008-01-09 Thread Fredrik Lundh
Paddy wrote: To see how they act against 'corner cases' and an exercise for me in trying to create corner cases. (I'm in to functional testing at the mo'). sounds more like pulling requirements out of thin air. not sure that helps the OP get a better understanding of Python, really. /F --

Re: alternating string replace: Extended input (Long).

2008-01-09 Thread Paddy
On Jan 9, 9:39 pm, Fredrik Lundh [EMAIL PROTECTED] wrote: Paddy wrote: To see how they act against 'corner cases' and an exercise for me in trying to create corner cases. (I'm in to functional testing at the mo'). sounds more like pulling requirements out of thin air. not sure that

Re: alternating string replace

2008-01-09 Thread Gordon C
This is very cool stuff but I suspect that the code is unreadable to many readers, including me. Just for fun here is a complete program, written in Turbo Pascal, circa 1982, that does the job. Readable n'est pas? Program dash; var str: string[80]; n: integer; odd: boolean; begin

Re: alternating string replace

2008-01-09 Thread bearophileHUGS
Gordon C: This is very cool stuff but I suspect that the code is unreadable to many readers, including me. Just for fun here is a complete program, written in Turbo Pascal, circa 1982, that does the job. Readable n'est pas? I think it's quite readable, especially if you indent it more