473,383 Members | 1,880 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,383 software developers and data experts.

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 2437
On 2008-08-17, Alexnb <al********@gmail.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('no'):
if 'yes' in s:
j = s.index('yes')
print s[j+4:]

Aug 17 '08 #4
On Aug 17, 6:03�pm, B <execra...@gmail.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('no'):
� � �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...@gmail.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('no'):
� � �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('no'):
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...@gmail.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
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:...
3
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...
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...
15
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...
6
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
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...
15
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...
9
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
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: 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...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.