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 advance
Cesco 33 2781
On Jan 9, 11:34 am, cesco <fd.calabr...@g mail.comwrote:
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 advance
Cesco
For replacing every other one, I tend to use the modulo, with a
counter.
count = (if u want even start with 0 else do 1)
while/for replacing /in lalastring:
if count % 2 == 0:
do even
else:
do odd
count += 1
This is a rather basic way of doing it, and I am sure people will sure
much more efficient ways, but it generally works for me.
Though I am now wondering if count % 2 when count == 2 is true or
false as it returns 0
Of course you still need to do your replace but you should be able to
do that.
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 finally joining
the result with "," ?
>>s1 = "hi_cat_bye_dog " s1 = s1.split("_") s1
['hi', 'cat', 'bye', 'dog']
>>s1 = [s1[i]+":"+s1[i+1] for i in range(0,len(s1) ,2)] s1
['hi:cat', 'bye:dog']
>>s1 = ",".join(s1 ) s1
'hi:cat,bye:dog '
(there are many other ways to do it, but the above 3-liner is short and
straightforward . note the use of range() to step over every other item
in the list)
</F>
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(":,").n ext: c(), "hi_cat_bye_dog ")
'hi:cat,bye:dog '
Is there a common recipe to accomplish that? I can't come up with any
solution...
There are many. If you want to learn Python don't be afraid to write it
in a long-winded way (with loops and helper functions) first.
Peter
cesco <fd**********@g mail.comwrote:
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...
Here's yet another answer:
from itertools import islice, cycle
def interleave(*ite rators):
iterators = [ iter(i) for i in iterators ]
while 1:
for i in iterators:
yield i.next()
def punctuate(s):
parts = s.split('_')
punctuation = islice(cycle(': ,'), len(parts)-1)
return ''.join(interle ave(parts, punctuation))
s1 = 'hi_cat_bye_dog '
print punctuate(s1)
# Or as a one-liner (once you have interleave):
print ''.join(list(in terleave(s1.spl it('_'), cycle(':,')))[:-1])
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 evenOrOdd
else:
s2 += i
print s2
On Jan 9, 2008 5:34 AM, cesco <fd**********@g mail.comwrote:
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 advance
Hum, hum... If I had a hammer...
from pyparsing import *
word = Word(alphas)
sep = Literal('_').su ppress()
pair = Group(word + sep + word)
pairs = delimitedList(p air, '_')
print ','.join(':'.jo in(t) for t in
pairs.parseStri ng('hi_cat_bye_ dog').asList())
--
Neil Cerutti <mr************ ***@gmail.com>
On Jan 9, 2008 5:34 AM, cesco <fd**********@g mail.comwrote:
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 advance
Hum, hum... If I had a hammer...
from pyparsing import *
word = Word(alphas)
sep = Literal('_').su ppress()
pair = Group(word + sep + word)
pairs = delimitedList(p air, '_')
print ','.join(':'.jo in(t) for t in
pairs.parseStri ng('hi_cat_bye_ dog').asList())
--
Neil Cerutti <mr************ ***@gmail.com>
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
solution...
What about :
>>import re s1 = 'hi_cat_bye_dog ' print re.sub(r'([^_]+)_([^_]+)_?', r'\1:\2;', s1)
hi:cat;bye:dog;
it still works for a longer string:
>>s1 = 'hi_cat_bye_dog _' + s1 s1
'hi_cat_bye_dog _hi_cat_bye_dog '
>>print re.sub(r'([^_]+)_([^_]+)_?', r'\1:\2;', s1)
hi:cat;bye:dog; hi:cat;bye:dog; This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: bettyann |
last post by:
i am using templates via HTML_Template_IT to create a table from the
results of a mysql query. i wanted to change the background image of
alternating rows in the table. i thought i could do this by defining
two different blocks in the template file and setting the current block
based on ( $thisRowNum % 2 ).
when the final table is...
|
by: Eirik Eldorsen |
last post by:
I'm trying to set alternating bgcolor for a datalist with 2 columns. My
problem is that its the alternating cell that get the bgcolor, not the row.
Is it possible to set alternating color of rows?
Here is my code:
<asp:DataList id=dlKommuner runat="server" Width="400px"
RepeatColumns="2" CellPadding="3">
<AlternatingItemStyle...
|
by: Max Weebler |
last post by:
Hi,
I have a datagrid built that has an alternating item style that sets the
backcolor and ForeColor of its rows. I have 4 template columns. One of them
has a LinkButton embedded in it to perform updates. All the styles that are
set are being followed by all templated columns with the exception for the
update column. The update column...
|
by: Daniel Manes |
last post by:
My DataGridView is set up like this:
* Main row background color = white
* Alternating row background color = light gray
I also have a read-only (non-editable) column I want to show up as
solid dark gray (i.e., no alternating row colors), but it shows up as
alternating dark/light gray.
Is there any way to get around this?
|
by: mike |
last post by:
how can I change the font color for an alternating row where the
column data is formatted as a link?
setting a style in the stylesheet for
a {
color:white;
}
| |
by: Flubleah |
last post by:
Hi there,
I'm trying to create a function to make alternating uppercase and lowercase letters of a string using loops. e.g: funny = FuNnY or judge = JuDge
Dim a As String = TextBox9.Text
Dim b As String
Dim d As String
For i As Integer = 0 To a.Length - 1 Step 2
Dim c As String = a(i)
...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
| |
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |