How to iterate the input over a particular size?

John Posner jjposner at optimum.net
Tue Dec 29 00:49:50 EST 2009


On Sun, 27 Dec 2009 09:44:17 -0500, joy99 <subhakolkata1234 at gmail.com>  
wrote:

> Dear Group,
>
> I am encountering a small question.
>
> Suppose, I write the following code,
>
> input_string=raw_input("PRINT A STRING:")
> string_to_word=input_string.split()
> len_word_list=len(string_to_word)
> if len_word_list>9:
>              rest_words=string_to_word[9:]
>              len_rest_word=len(rest_words)
>              if len_rest_word>9:
>                       remaining_words=rest_words[9:]
>

Here's an issue that has not, I think, been addressed in this thread. The  
OP's problem is:

1. Start with an indefinitely long string.

2. Convert the string to a list, splitting on whitespace.

3. Repeatedly return subslices of the list, until the list is exhausted.

This thread has presented one-chunk-at-a-time (e.g. generator/itertools)  
approaches to Step #3, but what about Step #2? I've looked in the Python  
documentation, and I've done some Googling, but I haven't found a  
generator version of the string function split(). Am I missing something?

Tx,
John





More information about the Python-list mailing list