469,592 Members | 2,030 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,592 developers. It's quick & easy.

like a "for loop" for a string


Okay, so lets say you have a list:

funList = [1,2,3,4,5]

and you do:

for x in funList:
print x

this will print 1-5
But I am wondering is there a way to something like this:

funString = "string string string non-string non-string string"
and
for "string" in funString:
print something

I know you can't do that; but, is there a way do do something similar that
gets the same result?
--
View this message in context: http://www.nabble.com/like-a-%22for-...p19022098.html
Sent from the Python - python-list mailing list archive at Nabble.com.

Aug 17 '08 #1
9 1359
Alexnb wrote:
Okay, so lets say you have a list:

funList = [1,2,3,4,5]

and you do:

for x in funList:
print x

this will print 1-5
But I am wondering is there a way to something like this:

funString = "string string string non-string non-string string"
and
for "string" in funString:
print something

I know you can't do that; but, is there a way do do something similar that
gets the same result?
Your funString has six elements, funList
has five.

How do you want the result to appear on
the page?

It is possible that the % operator could
help you.

Colin W.
Aug 17 '08 #2
On Sun, 17 Aug 2008 11:22:37 -0700 (PDT), Alexnb wrote:
funString = "string string string non-string non-string string"
and
for "string" in funString:
print something

I know you can't do that; but, is there a way do do something similar that
gets the same result?
What's "that"?

Do you mean _this_:
>>somestr = "string1 string2 string3"
for i in somestr.split():
.... print i
....
string1
string2
string3
>>>
?

--
Regards,
Wojtek Walczak,
http://www.stud.umk.pl/~wojtekwa/
Aug 17 '08 #3
Wojtek Walczak:
>somestr = "string1 string2 string3"
for i in somestr.split():

... print i
...
string1
string2
string3
I'm waiting for a str.xsplit still :-)
If I write and submit a C implementation of xsplit how many chances do
I have to see it included into Python? :-)

Bye,
bearophile
Aug 17 '08 #4
So what exactly does that do? Returns a generator, instead of a list?

I'm waiting for a str.xsplit still :-)
If I write and submit a C implementation of xsplit how many chances do
I have to see it included into Python? :-)

Bye,
bearophile
--
http://mail.python.org/mailman/listinfo/python-list
Aug 17 '08 #5
Eric Wertman:
So what exactly does that do? Returns a generator, instead of a list?
Allows you to iterate on the parts in a lazy way, without creating the
whole list of the pieces.
The arguments are the same of str.split().

Bye,
bearophile
Aug 17 '08 #6
On 17 Aug, 20:22, Alexnb <alexnbr...@gmail.comwrote:
But I am wondering is there a way to something like this:

funString = "string string string non-string non-string string"
and
for "string" in funString:
print something

I know you can't do that; but, is there a way do do something similar that
gets the same result?
Perhaps this...

for s in funString.split():
if s == "string":
print something

Assuming that you want to iterate over the separate words in funString
(for which you need to use the split method on funString), and that
you only want to print something when the word being considered is
"string".

To understand why this may (or may not) be what you want, try and
articulate what needs to happen. You appear to want to treat funString
like the list of numbers, and it could be that you consider the
boundaries between the "elements" in funString to be spaces (although
you don't say). So, first we need to get a list which meets your
requirements:

funString.split() # ["string", "string", "string", "non-
string", ...]

Then, you want to visit the elements in this list, perhaps - that's
where the "for" loop comes in. However, you use "string" as the loop
variable which isn't going to work (as you may already have
discovered). So let's use a variable instead:

for s in funString.split():
print s

This will print all the elements in that list. It appears that you
only want to consider "string", however, so that means that you need
to test the value of s and to only do something if s is equal to
"string" - that's where the "if" statement comes in.

When you have data which doesn't immediately fit into an existing
piece of code, don't be afraid to experiment at the Python prompt and
to try and turn your data into something the existing code can use.
And don't expect magic: if your first instinct produces a syntax error
('for "string" in funString') consider what you are trying to express
and then try and find language constructs to express it.

Paul
Aug 17 '08 #7
On Sun, 17 Aug 2008 12:07:45 -0700 (PDT), be************@lycos.com wrote:
I'm waiting for a str.xsplit still :-)
If I write and submit a C implementation of xsplit how many chances do
I have to see it included into Python? :-)
Got no idea, but it might be a nice try. It should be a quite good memory
saver for very large strings. While browsing the implementation of
split method I discovered something I didn't really realise before.
While calling split method without arguments it treats all four signs:
' ', '\n', '\r' and '\t' as a whitespace, so it's not the same as
str.split(' '). Moreover, specifying maxsplit argument whenever possible
seems to be a good practice. Anyway, go ahead with that xsplit thing :-)

--
Regards,
Wojtek Walczak,
http://www.stud.umk.pl/~wojtekwa/
Aug 17 '08 #8
Wojtek Walczak wrote:
Got no idea, but it might be a nice try. It should be a quite good memory
saver for very large strings.
or you could use re.finditer, which can be used for a lot more than just
splitting.

</F>

Aug 18 '08 #9
Fredrik Lundh:
or you could use re.finditer, which can be used for a lot more than just
splitting.
Having implemented some of them (and even invented algorithms like a
new substring search, that I have appreciated) you know that string
methods are simpler ways to efficiently perform specialized functions
that are used often. Many string methods can be replaced by operations
with REs, but they become less easy/clean to use, and maybe even
slower. A specialized xsplit can be faster than re.finditer, and the
syntax is better/handier.

Bye,
bearophile
Aug 18 '08 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Noah | last post: by
34 posts views Thread by Frederick Gotham | last post: by
29 posts views Thread by not.here.now | last post: by
7 posts views Thread by copx | last post: by
1 post views Thread by Alexnb | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.