Browse Source

Adding recursive generators using the "yield from" statment

pull/53/head
dbecad 4 years ago
parent
commit
b089608e2e
1 changed files with 17 additions and 0 deletions
  1. 17
      README.md

17
README.md

@ -238,7 +238,24 @@ def count(start, step):
>>> next(counter), next(counter), next(counter)
(10, 12, 14)
```
### Recursive generator
* **Generators can be recursive using the yield from statment.**
```python
def depth_first(trie, current_word = ''):
if trie is None:
yield current_word
else:
for letter, new_trie in trie.items():
yield from depth_first(new_trie, current_word + letter)
```
```python
>>> trie={'b': {'a': {'b': {'y': None}, 'd': None,'n': {'k': None}}, 'o': {'x': None}},
... 't': {'e': {'a': None,'d': None,'n': None}, 'o': None}}
>>> [w for w in depth_first(trie)]
['baby', 'bad', 'bank', 'box', 'tea', 'ted', 'ten', 'to']
```
Type
----

Loading…
Cancel
Save