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
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
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
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
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:
-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
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 =
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
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
--
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
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)
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
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
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
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
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:
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
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
--
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
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
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
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
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 \
__ ___
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
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
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
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'.
--
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
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
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
--
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
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
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
33 matches
Mail list logo