like a "for loop" for a string
execrable at gmail.com
Mon Aug 18 09:04:25 CEST 2008
> On Aug 17, 6:03�pm, B <execra... at gmail.com> wrote:
>> 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
>> 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
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() # ''
More information about the Python-list