Generators
- An iterable is any sequence we can iterate over (we may clal iter() on the iterator and get an iterator)
- An iterator allows us to iterate over any iterable sequence (we can call next() on it and get the next item in the sequence)
- iter()
- enumerate()
- map()
- zip()
- range()
1
2
3
4
5
6
7
8
9
10
11
12
13
from tree import *
def exclude(t, x):
filtered_branches = map(lambda y: exclude(y, x), branches(t))
bs = []
for b in filtered_branches:
if label(b) == x:
bs.extend(branches(b))
else:
bs.append(b)
return tree(label(t), bs)
t = tree(1, [tree(2, [tree(2), tree(3), tree(4)]), tree(5, [tree(1)])])
exclude(t, 2)
1
[1, [3], [4], [5, [1]]]