Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- from collections import deque
- from itertools import chain, islice
- from operator import ne
- def dropuntil(iterable, needle):
- """
- Advance `iterable` until a sequence with all items of `needle` (with
- respect to their order) is found. Then yield all remaining items
- starting with the first item of `needle`. Yield nothing if `iterable`
- does not contain the given needle.
- """
- iterator = iter(iterable)
- window = deque(maxlen=len(needle))
- window.extend(islice(iterator, len(needle)))
- if len(window) == len(needle):
- while any(map(ne, window, needle)):
- try:
- window.append(next(iterator))
- except StopIteration:
- return
- yield from chain(window, iterator)
- def main():
- print(list(dropuntil([1,2,9,3,4,3,5,2,3,4,2,3], [2,3])))
- print(list(dropuntil([1,2,9,3,4,3,5,2,3,4,2,3], [8,9])))
- print(list(dropuntil([1,2,9,3,4,3,5,2,3,4,2,3], [])))
- print(''.join(dropuntil('xxxhamyyyspamzzzeggs', 'spam')))
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement