Example with a generator:
It has no big difference compared to the previous generator-function.
- assign
False
tostart_found
- iterate line by line, which should be word for word
- if start was found, change
start_found
toTrue
- yield element if
start_found
isTrue
- return from generator, if end is the element. This will also leave the for-loop
- optional Exceptions:
- if the for-loop was finished, but
start_found
is stillFalse
, then start-word was not found
- if the for-loop was finished and
start_found
isTrue
, then the end-word was not found
- if the for-loop was finished, but
word_start = "yes" word_end = "no" words = """yes ok ok ok no""".splitlines() def split(sequence, start, end): start_found = False for element in sequence: if element == word_start and not start_found: start_found = True elif element == word_end and start_found: return # close generator elif start_found: yield element # this point is reached, if the start or end was not found if start_found: # seen start, but no end raise ValueError(f"'{end}' was not the last element in sequence") else: # seen no start in the whole sequence raise ValueError(f"The start_word '{start}' was not found in sequence") oks = list(split(words, word_start, word_end)) print(oks)Here the Version, which includes start-word and stop-word.
It has no big difference compared to the previous generator-function.
word_start = "yes" word_end = "no" words = """yes ok ok ok no""".splitlines() def split(sequence, start, end): start_found = False for element in sequence: if element == word_start and not start_found: start_found = True yield element elif element == word_end and start_found: yield element # yield the word_end return # close generator elif start_found: yield element # this point is reached, if the start or end was not found if start_found: # seen start, but no end raise ValueError(f"'{end}' was not the last element in sequence") else: # seen no start in the whole sequence raise ValueError(f"The start_word '{start}' was not found in sequence") oks = list(split(words, word_start, word_end)) print(oks)
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
All humans together. We don't need politicians!