python itertools with islice error
Question: I'm still learning python and I have the code below but it is not working:

    
    
    from itertools import *
    
    startword = ["start",]
    stopword = ["stop",]
    text = "this is a text that starts with some test stuff and then after that it stop right here!"
    
    for i in islice(text.split(" "), startword, stopword):
        print i
    

I'm trying to print the words between the start and stop without knowing how
many words are there in between. from the error I'm getting it look like I
need an integer for start and stop parameters for islice. here is the error
"`ValueError: Indices for islice() must be None or an integer: 0 <= x <=
maxing.`" any other itertool that I can use?!

Thanks,


Answer: In this particular case, `islice` is a poor choice; you have all the data in
memory, in a realized sequence, so `islice` is just wasting time iterating the
leading values. There are many better ways to handle this, either using
`index` to find the `start` and `end` indices and doing a true slice, or to
get more clever, and reduce the work by splitting out the `start/end`
delimited section and only splitting that part to extract words. For example:

    
    
    text = "this is a text that starts with some test stuff and then after that it stop right here!"
    
    _, text = text.split('start', 1)  # Remove start and stuff before it
    text, _ = text.rsplit('stop', 1)  # Remove stop and stuff after it
    
    for word in text.split():  # Split what remains on whitespace
        print word
    

Mind you, this still isn't quite right (your bounded region begins with
"starts" not "start", so you end up with a leading "word" of "s"), but
switching to `re.split` with appropriate bounding and wildcarding could be
used to fix that in whatever way is appropriate to your scenario.

