473,714 Members | 2,552 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1565
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...@gma il.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
4010
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 field sizes in crystal reports, but still get the same error. I create a new crystal report using Visual C# and then added a Windows Form Viewer in a Windows Form. I then hosted the crystal report on the Windows Form Viewer.
8
2033
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 label, etc. What I need to do is create a JS validation routine that will check each control for valid input, regardless of what the control name is. If it is a "select", it needs to verify the index is > 1. If it is an "input", it needs to...
13
11018
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 below would work... string gsub(const string & sData, const string & sFrom,
2
1517
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 to have the criteria met if the string "Small" is anywhere in the field. So any of these would qualify: Short Small Medium Small Small
34
2675
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 <limits.h> #include <stdlib.h> int main(void) {
29
1997
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. Can someone tell me why "->" exists? The compiler knows the difference between a structure and a pointer to a structure, so why can't it just let me write "foo.bar" in both cases and not have to go back and rewrite things when I later decide I want...
7
7747
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 also be interested in whether or not ISO C99 changed the limits (if any exist).
1
165
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 string string non-string string" Does that help? Fredrik Lundh wrote: --
0
8798
marktang
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
9073
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9013
tracyyun
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7947
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6632
isladogs
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4463
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4719
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3156
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
3
2108
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.