472,353 Members | 1,232 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

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 1463
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: habdalla | last post by:
When loading crystal reports in my windows applications(created using Visual C#), I get an error "Buffer too small for string or missing null byte"....
8
by: Calan | last post by:
I have a server-side ASP script that dynamically creates an input form from a database table. The table contains a field name, the table where...
13
by: M | last post by:
Hi, I've searched through the previous posts and there seems to be a few examples of search and replacing all occurrances of a string with...
2
by: Noah | last post by:
If I want to do an update query with the critera being that a field contains a certain string anywhere within, how would I write that? Would that...
34
by: Frederick Gotham | last post by:
Is the domestic usage of the C "for" loop inefficient when it comes to simple incrementation? Here's a very simple program that prints out the...
29
by: not.here.now | last post by:
A quick search of this group and its FAQ, and elsewhere, have not answered this question to my satisfaction. Apologies if I missed something...
7
by: copx | last post by:
Do the standards say anything about size limits for string literals (min size, max size)? I want to know this to make sure that my code is portable....
1
by: Alexnb | last post by:
Basically I want the code to be able to pick out how many strings there are and then do something with each, or the number. When I say string I mean...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.