I heard that strings are immutable, but isn't there regardless a way to
manipulate a string?
I have a string that looks like this:
a = '0123456789'
But I want it to look like this:
a = '0 - 2 - 4 - 6 - 8 - '
I want whitespace between every number and I want to fade out every
second number.
It is funny that I can explicitly read parts of that string, print a[3]
works perfectly, but I can't remove it, delete it or what ever.
I would appreciate any hint on how to solve my little "problem".
Thanks a lot.
Regards, Tom 14 3859
Michel Claveau/Hamster wrote: Like this :
import string
a='0123456789' l=list(a) b=string.join(l,' - ') print b
Apparently, the OP wants to discard characters at indexes 1, 3, 5, 7, etc... So,
assuming Python version is 2.3, the correct solution seems to be: a='0123456789' print ' - '.join(a[::2])
0 - 2 - 4 - 6 - 8
HTH
--
- Eric Brunel <eric dot brunel at pragmadev dot com> -
PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com
Oups ! Sorry, i had read too fast...
2 by 2 :
import string
a='0123456789'
l=list(a[::2])
b=string.join(l,' - ')
print b
Thanks guys.
I knew it couldn't be too difficult, but I just didn't know in which
direction I should search for a solution. :-)
Thanks again.
Regards, Tom
It is me again.
Unfortunately it doesn't work. Python doesn't accept [::2]
TypeError: sequence index must be integer
I use Python Release 2.2.3.
Any ideas?
-Tom
> import string a='0123456789' l=list(a[::2]) b=string.join(l,' - ') print b
Chris wrote:
It is me again. Unfortunately it doesn't work. Python doesn't accept [::2] TypeError: sequence index must be integer I use Python Release 2.2.3. Any ideas?
-Tom
Yeah, the x[::i] notation for strings and lists only works in 2.3.
A couple of alternatives to l = list(a[::2]) which work in 2.2 are:
l = []
for i in range( (len(a) + 1) // 2):
l.append(a[2*i])
or more compactly
l=[a[2*i] for i in range((len(a)+1)//2)]
or
l = [a[i] for i in range(len(a)) if 2*(i//2) == i]
David
David C. Fox wrote: import string a='0123456789' l=list(a[::2]) b=string.join(l,' - ') print b
Chris wrote:
It is me again. Unfortunately it doesn't work. Python doesn't accept [::2] TypeError: sequence index must be integer I use Python Release 2.2.3. Any ideas?
-Tom
Yeah, the x[::i] notation for strings and lists only works in 2.3.
A couple of alternatives to l = list(a[::2]) which work in 2.2 are:
l = [] for i in range( (len(a) + 1) // 2): l.append(a[2*i])
or more compactly
l=[a[2*i] for i in range((len(a)+1)//2)]
or
l = [a[i] for i in range(len(a)) if 2*(i//2) == i]
David
or
l = [a[i] for i in range(0, len(a), 2)]
David
Hi,
Eric Brunel wrote: Apparently, the OP wants to discard characters at indexes 1, 3, 5, 7, etc... So, assuming Python version is 2.3, the correct solution seems to be:
>>> a='0123456789' >>> print ' - '.join(a[::2]) 0 - 2 - 4 - 6 - 8
almost :-)
the original poster said he wanted
a = '0 - 2 - 4 - 6 - 8 - '
which means the last faded-out 9 is missing in your solution
the following works: a = '0123456789'
b = " ".join( [i % 2 and "-" or a[i] for i in range(len(a))] ) b
'0 - 2 - 4 - 6 - 8 -'
I think the trailing space is a typo of the OP...
hth
Werner
Hi,
Werner Schiendl wrote: the following works:
>>> a = '0123456789' >>> >>> b = " ".join( [i % 2 and "-" or a[i] for i in range(len(a))] ) >>> b '0 - 2 - 4 - 6 - 8 -'
if you prefer to use generators, you could use: a = '0123456789'
def hide_every_2nd(x):
.... while True:
.... yield x.next()
.... x.next()
.... yield "-"
.... b = " ".join(hide_every_2nd(iter(a))) b
'0 - 2 - 4 - 6 - 8 -'
hth
Werner
On Tue, Oct 28, 2003 at 07:06:36PM +0100, Chris wrote: It is me again. Unfortunately it doesn't work. Python doesn't accept [::2] TypeError: sequence index must be integer I use Python Release 2.2.3. Any ideas?
Then, s = "0123456789" " - ".join([s[i] for i in range(0, 10, 2)])
'0 - 2 - 4 - 6 - 8'
-Inyeol
Thanks for all the help.
I just decided to update to Python 2.3 anyway.
Thanks to all, Tom s = "0123456789" " - ".join([s[i] for i in range(0, 10, 2)])
'0 - 2 - 4 - 6 - 8'
I thought the original poster wanted the odd numbers replaced by '-' and then
all elements separated by spaces. None of the ' - '.join(...) examples
achieve that, as they fail to replace '9' with '-'. Here's an alternative
which does:
s = "0123456789" [int(c)%2 and '-' or c for c in s]
['0', '-', '2', '-', '4', '-', '6', '-', '8', '-'] ' '.join([int(c)%2 and '-' or c for c in s])
'0 - 2 - 4 - 6 - 8 -'
Skip This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Paul |
last post by:
This method:
static void swap(int a, int b) {
int c = a;
a = b;
b = c;
}
does not swap the values of a and b over. It doesn't work for String
variables either. However the following method...
|
by: crovax75 |
last post by:
LS,
I've a problem to get the year, month and day from the following
string: "20051027"
--
#include <stdio.h>
#include <string.h>
|
by: Greg Scharlemann |
last post by:
If I have a date that looks like: 2005-12-07 10:10:00
How could I manipulate it in php to say "Dec, 07, 2005"?
I can separate the string at the space, but don't know where to go from...
|
by: tom |
last post by:
Hi,
I am a newbie and have a simple question?
I have a aspx file and on the form I have a user control which is my
header and has some images. Now for various pages I need to load
different...
|
by: G |
last post by:
Hello,
Does anyone have any code handy which will take a certain part of a string
and save it as an independant string?
For example:
"Thank you for visiting our web site. You were sent...
|
by: Bart |
last post by:
Hi,
i want to programmatically manipulate the property 'Name' of a
ControlParameter inside a InsertParameters tag.
This the aspx code:
------------------
<asp:SqlDataSource...
|
by: redx |
last post by:
void addition::print_output()
{
cout << "Enter value : ";
getline(cin,store);
}
-----------------------------------------------------------------------
here my problem,
for e.g : if user...
|
by: sachin770 |
last post by:
i am a beginner .can you give me idea how we can manipulate "LARGE" integers (positive or negative) of arbitrary size. These integers could be greater than MAX_INT or smaller than MIN_INT.
using...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
| |