from __future__ import generator_stop
import itertools
def _takewhile(datapredicate, valueiterator, has_data):
"""
Return successive entries from an iterable as long as the
predicate evaluates to true for each entry.
has_data outputs if the iterator has been consumed in the process.
"""
for item in dataiterator:
if predicate(item != value):
yield item
else:
break
else:
has_data[0] = False
def isplit(dataiterator, value):
data"""Return a lazy generator of items in an iterator, seperating by value."""
iterator = iter(dataiterator)
has_data = [True]
while has_data[0]:
yield _takewhile(datavalue.__ne__, valueiterator, has_data)
def split(dataiterator, value):
data"""Return a semi-lazy generator of items in an iterator, seperating by value."""
iterator = iter(dataiterator)
try:
has_data = [True]
while True:
carry = []
d = itertools.takewhile_takewhile(value.__ne__, dataiterator, has_data)
try:
first = next(d)
except StopIteration:
if not has_data[0]:
break
yield itertools.chainiter([first],[])
d, carry) else:
carryyield itertools.extendchain([first], d, carry)
except StopIteration:
passcarry.extend(d)
print('isplit')
print([list(i) for i in isplit('abc def ghi', ' ')])
print([list(i) for i in isplit(' abc def ghi', ' ')])
s = isplit('abc def ghi', ' ')
print(list(itertools.zip_longest(*itertools.islice(s, 4))))
print('\nsplit')
print([list(i) for i in split('abc def ghi', ' ')])
print([list(i) for i in split(' abc def ghi', ' ')])
s = split('abc def ghi', ' ')
print(list(itertools.zip_longest(*itertools.islice(s, 4))))
isplit
[['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']]
[[], ['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']]
[('a', 'b', 'c', None), ('d', 'e', 'f', None), (None, 'g', 'h', None), (None, 'i', None, None)]
split
[['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']]
[[], ['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']]
[('a', 'd', 'g'), ('b', 'e', 'h'), ('c', 'f', 'i')]