Guest User

Untitled

a guest
Feb 21st, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. from __future__ import generator_stop
  2. import itertools
  3.  
  4.  
  5. def _takewhile(data, value, has_data):
  6. for item in data:
  7. if item != value:
  8. yield item
  9. else:
  10. break
  11. else:
  12. has_data[0] = False
  13.  
  14.  
  15. def isplit(data, value):
  16. data = iter(data)
  17. has_data = [True]
  18. while has_data[0]:
  19. yield _takewhile(data, value, has_data)
  20.  
  21.  
  22. def split(data, value):
  23. data = iter(data)
  24. try:
  25. while True:
  26. carry = []
  27. d = itertools.takewhile(value.__ne__, data)
  28. first = next(d)
  29. yield itertools.chain([first], d, carry)
  30. carry.extend(d)
  31. except StopIteration:
  32. pass
  33.  
  34. print('isplit')
  35. print([list(i) for i in isplit('abc def ghi', ' ')])
  36. s = isplit('abc def ghi', ' ')
  37. print(list(itertools.zip_longest(*itertools.islice(s, 4))))
  38.  
  39. print('nsplit')
  40. print([list(i) for i in split('abc def ghi', ' ')])
  41. s = split('abc def ghi', ' ')
  42. print(list(itertools.zip_longest(*itertools.islice(s, 4))))
  43.  
  44. isplit
  45. [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']]
  46. [('a', 'b', 'c', None), ('d', 'e', 'f', None), (None, 'g', 'h', None), (None, 'i', None, None)]
  47.  
  48. split
  49. [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']]
  50. [('a', 'd', 'g'), ('b', 'e', 'h'), ('c', 'f', 'i')]
Add Comment
Please, Sign In to add comment