Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def extract_words(mixture):
- """
- Flat deep nested iterables and split strings if they occour.
- """
- stack = deque([mixture])
- # using a stack to allow iterating
- # deep nested list
- # it could be done easier with recursion
- # but all stack based languages have a recursion limit
- to_split = (str, bytes)
- # we want to split str and bytes
- while stack:
- # loop runs until stack is empty
- current = stack.popleft()
- # with the first iteration
- # stack is currently empty
- # and current has the first element from stack
- if isinstance(current, to_split):
- # split if the current object is a str or bytes
- yield from current.split()
- else:
- # this branch is executed, if the current object
- # is not a str or bytes
- try:
- current = iter(current)
- # iter of iter returns the same iterator
- subelement = next(current)
- # the next does what the for loop does
- except StopIteration:
- # but we have to check for errors manually
- pass
- except TypeError:
- # and if an element is not iterable, it raieses
- # TypeError. Intgers are for example are not
- # iterable
- yield subelement
- else:
- # if no error happens, put the current iterator back
- # to the left side of the stack
- stack.appendleft(current)
- # put the subelement of the beginning of the deque
- stack.appendleft(subelement)
- data = ['Test eins zwei drei', ['hallo', '123'], [[[[['foo bar']]], 'bat']], 12]
- extractor = extract_words(data)
- result = list(extractor)
- print(result)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement