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

Home Posts Topics Members FAQ

Re: like a "for loop" for a string


Uhm, "string" and "non-string" are just that, words within the string. Here
shall I dumb it down for you?
string = "yes text1 yes text2 yes text3 no text4 yes text5+more Text yes
text6 no text7 yes text8"

It doesn't matter what is in the string, I want to be able to know exactly
how many "yes"'s there are.
I also want to know what is after each, regardless of length. So, I want to
be able to get "text1", but not "text4" because it is after "no" and I want
all of "text5+more Text" because it is after "yes". It is like the yeses are
bullet points and I want all the info after them. However, all in one
string.
Fredrik Lundh wrote:
>
Alexnb wrote:
>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?

not really, since you haven't defined what "string" and "non-string" are
or how strings are separated from each other, and, for some odd
reason, refuse to provide an actual example that includes both a proper
sample string *and* the output you'd expect.

please don't use the mailing list to play 20 questions.

</F>

--
http://mail.python.org/mailman/listinfo/python-list

--
View this message in context: http://www.nabble.com/like-a-%22for-...p19022976.html
Sent from the Python - python-list mailing list archive at Nabble.com.

Aug 17 '08 #1
6 2496
On 2008-08-17, Alexnb <al********@gma il.comwroted:
string = "yes text1 yes text2 yes text3 no text4 yes text5+more Text yes
text6 no text7 yes text8"

It doesn't matter what is in the string, I want to be able to know exactly
how many "yes"'s there are.
----- cut here -----
>>import re
foo = "yes text1 yes text2 yes text3 no text4 yes text5+more Text yes
text6 no text7 yes text8"
results = re.findall("yes ", foo)
len(results )
6
>>results
['yes', 'yes', 'yes', 'yes', 'yes', 'yes']
----- cut here -----
I also want to know what is after each, regardless of length. So, I want to
be able to get "text1", but not "text4" because it is after "no" and I want
all of "text5+more Text" because it is after "yes". It is like the yeses are
bullet points and I want all the info after them. However, all in one
string.
I guess this can be done with regular expressions:

http://www.python.org/doc/current/lib/module-re.html

Read about groups, then write an appropriate regex.

GS
--
Grzegorz Staniak <gstaniak _at_ wp [dot] pl>
Aug 17 '08 #2
Am Sun, 17 Aug 2008 13:12:36 -0700 schrieb Alexnb:
Uhm, "string" and "non-string" are just that, words within the string.
Here shall I dumb it down for you?
Please, bear with us. You are deep into the problem, we are not.
It doesn't help to be rude. If you can explain your problem well, you are
halfway through to the solution.
>
string = "yes text1 yes text2 yes text3 no text4 yes text5+more Text yes
text6 no text7 yes text8"

It doesn't matter what is in the string, I want to be able to know
exactly how many "yes"'s there are.
I also want to know what is after each, regardless of length. So, I want
to be able to get "text1", but not "text4" because it is after "no" and
I want all of "text5+more Text" because it is after "yes". It is like
the yeses are bullet points and I want all the info after them. However,
all in one string.
How about this:
>>s="yes t1 yes t2 no t3 yes t4 no t5"
l=s.split()
l
['yes', 't1', 'yes', 't2', 'no', 't3', 'yes', 't4', 'no', 't5']
>>for i in range(1,len(l), 2):
.... if l[i-1] == 'yes': print l[i]
t1
t2
t4
>>>
Now your problem is reduced to splitting the input into (yes/no) and
(text) pairs. For a simple string like the one above split() is ok, but
string = "yes text4 yes text5+more Text yes text6 no text7"
will be split into [ ... 'yes', 'text5+more', 'Text', 'yes', ...],
breaking my simple algorithm.

You could look for the index() of 'yes' / 'no', but then you'd have to
make sure that 'text' does not contain 'yes' or 'no'.

HTH.
Martin

Aug 17 '08 #3
B

Alexnb wrote:
Uhm, "string" and "non-string" are just that, words within the string. Here
shall I dumb it down for you?
string = "yes text1 yes text2 yes text3 no text4 yes text5+more Text yes
text6 no text7 yes text8"

It doesn't matter what is in the string, I want to be able to know exactly
how many "yes"'s there are.
I also want to know what is after each, regardless of length. So, I want to
be able to get "text1", but not "text4" because it is after "no" and I want
all of "text5+more Text" because it is after "yes". It is like the yeses are
bullet points and I want all the info after them. However, all in one
string.

It seems like this is the type of thing the re module would be good at.
But for your example, this would work too:

for s in string.split('n o'):
if 'yes' in s:
j = s.index('yes')
print s[j+4:]

Aug 17 '08 #4
On Aug 17, 6:03�pm, B <execra...@gmai l.comwrote:
Alexnb wrote:
Uhm, "string" and "non-string" are just that, words within the string. Here
shall I dumb it down for you?
string = "yes text1 yes text2 yes text3 no text4 yes text5+more Text yes
text6 �no text7 yes text8"
It doesn't matter what is in the string, I want to be able to know exactly
how many "yes"'s there are.
I also want to know what is after each, regardless of length. So, I want to
be able to get "text1", but not "text4" because it is after "no" and I want
all of "text5+more Text" because it is after "yes". It is like the yeses are
bullet points and I want all the info after them. However, all in one
string.

It seems like this is the type of thing the re module would be good at.
� But for your example, this would work too:

for s in string.split('n o'):
� � �if 'yes' in s:
� � � � �j = s.index('yes')
� � � � �print s[j+4:]
Did you run this? Doesn't look very useful to me.

text1 yes text2 yes text3
text5+more Text yes text6
text8
Aug 18 '08 #5
B
Mensanator wrote:
On Aug 17, 6:03�pm, B <execra...@gmai l.comwrote:
>Alexnb wrote:
>>Uhm, "string" and "non-string" are just that, words within the string. Here
shall I dumb it down for you?
string = "yes text1 yes text2 yes text3 no text4 yes text5+more Text yes
text6 �no text7 yes text8"
It doesn't matter what is in the string, I want to be able to know exactly
how many "yes"'s there are.
I also want to know what is after each, regardless of length. So, I want to
be able to get "text1", but not "text4" because it is after "no" and I want
all of "text5+more Text" because it is after "yes". It is like the yeses are
bullet points and I want all the info after them. However, all in one
string.
It seems like this is the type of thing the re module would be good at.
� But for your example, this would work too:

for s in string.split('n o'):
� � �if 'yes' in s:
� � � � �j = s.index('yes')
� � � � �print s[j+4:]

Did you run this? Doesn't look very useful to me.

text1 yes text2 yes text3
text5+more Text yes text6
text8

No, but it's hard to tell what's 'useful' when the original poster
wasn't really clear in what he was looking for. If you're referring to
the extra 'yes's, then you can easily fix that with another split:

for s in string.split('n o'):
if 'yes' in s.strip(): # don't want spaces around yes/nos?
j = s.index('yes')
for y in s[j+3:].split('yes'):
print y.strip() # ''

Aug 18 '08 #6
On Aug 17, 3:12*pm, Alexnb <alexnbr...@gma il.comwrote:
Uhm, "string" and "non-string" are just that, words within the string. Here
shall I dumb it down for you?

string = "yes text1 yes text2 yes text3 no text4 yes text5+more Text yes
text6 *no text7 yes text8"

It doesn't matter what is in the string, I want to be able to know exactly
how many "yes"'s there are.
I also want to know what is after each, regardless of length. So, I want to
be able to get "text1", but not "text4" because it is after "no" and I want
all of "text5+more Text" because it is after "yes". It is like the yeses are
bullet points and I want all the info after them. However, all in one
string.

Fredrik Lundh wrote:
Alexnb wrote:
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?
not really, since you haven't defined what "string" and "non-string" are
* *or how strings are separated from each other, and, for some odd
reason, refuse to provide an actual example that includes both a proper
sample string *and* the output you'd expect.
please don't use the mailing list to play 20 questions.
</F>
--
http://mail.python.org/mailman/listinfo/python-list

--
View this message in context:http://www.nabble.com/like-a-%22for-...g-tp19022098p1...
Sent from the Python - python-list mailing list archive at Nabble.com.- Hide quoted text -

- Show quoted text -
Well, in "dumbing" this down, I think you actually put a little more
thought into your explanation. If you break this up at the "yes"
words:

string = "yes text1 yes text2 yes text3 no text4 yes text5+more Text
yes text6 no text7 yes text8"

Would this not generate:

yes text1
yes text2
yes text3 no text4
yes text5+more Text
yes text6 no text7
yes text8

So your output *would* return "text4" and "text7", but buried within
the body indicated by the previous "yes".

Or is "no" supposed to be some kind of suppression trigger? Should
"no" turn OFF matching until another "yes" is found? Gee, I guess I
didn't read anything like that in your original post.

Please dumb this down some more, so we can figure out just what the
heck you mean. (And I can't wait to go to a customer to do
requirements analysis, and just tell them we need to "dumb things
down" for them!)

-- Paul
Aug 18 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
3571
by: Invalid User | last post by:
While trying to print a none empty list, I accidentaly put an "else" statement with a "for" instead of "if". Here is what I had: if ( len(mylist)> 0) : for x,y in mylist: print x,y else: print "Empty list" which was supposed to be:
3
1876
by: Peter Olcott | last post by:
Everything in this program produces the correct results except the part dealing with the RealList. The "for" loop does not output the values that were input to the ALL.RealList. What is the correct syntax to for this? Thanks #include <stdio.h> #include <string> #include <vector> union AllType {
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.
15
2530
by: Robin Eidissen | last post by:
What I try to do is to iterate over two variables using template metaprogramming. I've specialized it such that when it reaches the end of a row ot starts on the next and when it reaches the last row it stops.. At least that's what I thought I did, but VC71 says "warning C4717: 'LOOP<0,1>::DO' : recursive on all control paths, function will cause runtime stack overflow". What's wrong? Here's the code: template<int M, int N>
6
71975
by: John Pass | last post by:
What is the difference between a While and Do While/Loop repetition structure. If they is no difference (as it seems) why do both exist?
1
1711
by: Wazza | last post by:
G'Day, I have a re-occurring problem and wish to design a framework or pattern to address it. I have some Ideas but I would like to do some brainstorming with my peers before commencing. The problem: Several computational tasks I am running for my PhD work require several weeks to complete and I will to be able to pause these tasks
15
2362
by: Steve | last post by:
I am having problems getting values out of an array. The array is set as a global array and values are pushed into it as they are read from a JSON file using a "for loop". When the "for loop" is finished I want to convert the array into a string which can be used by another function. My attempt to do this is not working. The script looks like this: heights=; function getElevationInter(latv,lngv) { var script =...
9
1566
by: Alexnb | last post by:
Okay, so lets say you have a list: funList = and you do: for x in funList: print x this will print 1-5
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: --
1
4588
by: robotlizz | last post by:
Hello - I am a brand new at Java and I am having a hard time with a program I have to turn in tomorrow. I can not get the 'Q' option to work and the loop goes on forever. I've tried to go over the tutorials but when I change it from here I then get compiling issues... Can someone explain what I've done wrong? import javax.swing.JOptionPane; public class LloydPage_Assignment5 { public static void main (String args) { // Enter Y to...
0
8801
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...
0
8707
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9314
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9174
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9015
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...
1
6634
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
4725
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3158
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
2110
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.