473,385 Members | 1,523 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 1547
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". I tried various things i.e. increasing the...
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 values are stored, type of input control, value for a...
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 another string. I would have thought that the code...
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 be a case of using "like"? For example I want...
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 bit-numbers in a byte. #include <stdio.h> #include...
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 obvious, either in the literature or my reasoning. ...
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. The program in question is ANSI C89, but I would...
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 how many "strings" are in the string "string...
1
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.