Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from __future__ import generator_stop
- import itertools
- def _takewhile(data, value, has_data):
- for item in data:
- if item != value:
- yield item
- else:
- break
- else:
- has_data[0] = False
- def isplit(data, value):
- data = iter(data)
- has_data = [True]
- while has_data[0]:
- yield _takewhile(data, value, has_data)
- def split(data, value):
- data = iter(data)
- try:
- while True:
- carry = []
- d = itertools.takewhile(value.__ne__, data)
- first = next(d)
- yield itertools.chain([first], d, carry)
- carry.extend(d)
- except StopIteration:
- pass
- print('isplit')
- 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', ' ')])
- 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', None), ('d', 'e', 'f', None), (None, 'g', 'h', None), (None, 'i', None, None)]
- split
- [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']]
- [('a', 'd', 'g'), ('b', 'e', 'h'), ('c', 'f', 'i')]
Add Comment
Please, Sign In to add comment