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
>
> >
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 Rubi
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
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
--
http://mail.
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 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:
>
> -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'
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
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
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 f
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 t
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 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 thi
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
cor
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
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 sur
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?
>
> >
>
> To see how they act against 'corner
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.
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?
>
>
To see how they act against 'corner cases' and
an exercise for me in trying to create corner cases. (I
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'.
--
http://mail.
Donald 'Paddy' McCarthy wrote:
> I created some more test strings and ran posters solutions against them.
the point being?
--
http://mail.python.org/mailman/listinfo/python-list
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
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
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" \
" __ _
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
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 reci
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 reci
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
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
--
http://mail.python.org/mailman/listi
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?
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=cycl
On Jan 9, 11: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 t
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 ":", an
33 matches
Mail list logo