Connecting Tech Pros Worldwide Forums | Help | Site Map

Re: like a "for loop" for a string

Boris Borcic
Guest
 
Posts: n/a
#1: Aug 18 '08
Alexnb wrote:
Quote:
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.

def list_of_yes_prefixed_possibly_no_suffixed(s) :
return (' '+s).split(' yes ')[1:]

def removed_no_suffixes(t) :
return t.split(' no ')[0]

infos = map(removed_no_suffixes,
list_of_yes_prefixed_possibly_no_suffixed(string))

how_many = len(infos)

or as a one-liner :

infos = [ t.split(' no ')[0] for t in (' '+string).split(' yes ')[1:]]
Quote:
>
>
Fredrik Lundh wrote:
Quote:
>Alexnb wrote:
>>
Quote:
>>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
>>
>>
>

Closed Thread