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. 6 2262
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>
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
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:]
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
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() # ''
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 This discussion thread is closed Replies have been disabled for this discussion. Similar topics
23 posts
views
Thread by Invalid User |
last post: by
|
3 posts
views
Thread by Peter Olcott |
last post: by
|
reply
views
Thread by habdalla |
last post: by
|
15 posts
views
Thread by Robin Eidissen |
last post: by
|
6 posts
views
Thread by John Pass |
last post: by
|
1 post
views
Thread by Wazza |
last post: by
|
15 posts
views
Thread by Steve |
last post: by
|
9 posts
views
Thread by Alexnb |
last post: by
|
1 post
views
Thread by Alexnb |
last post: by
| | | | | | | | | | | |