Example with a generator:
- assign
False
to start_found
- iterate line by line, which should be word for word
- if start was found, change
start_found
to True
- yield element if
start_found
is True
- 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 still False
, then start-word was not found
- if the for-loop was finished and
start_found
is True
, then the end-word was not found
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)