Functions on trees, that is, nested sequences.
Function | flatten |
Flatten a tree, that is, recursively concatenate nested sequences. |
Function | is |
Test whether a tree is a fork (instead of a leaf). |
Function | tree |
... |
Function | tree |
Map a function over the leaves of a tree. |
Function | tree |
The shape of a tree expressed as nested tuples of nothing. |
Function | tree |
Zip recursively. |
Variable | basestring |
Undocumented |
Flatten a tree, that is, recursively concatenate nested sequences. >>> flatten([1, [[2, 3], 4, 5], [[[[6]]]]]) [1, 2, 3, 4, 5, 6] >>> mixed = [1, [[2], 3], (4, 5), [(6,)], 'abc'] >>> flatten(mixed) [1, 2, 3, 4, 5, 6, 'a', 'b', 'c'] >>> flatten(mixed, leaves=(str,)) [1, 2, 3, 4, 5, 6, 'abc'] >>> flatten(mixed, leaves=(str, tuple)) [1, 2, 3, (4, 5), (6,), 'abc'] >>> flatten([]) [] >>> flatten(1) [1]
... >>> tree_embeds([1], [2]) True >>> tree_embeds([1], [1,2]) False >>> tree_embeds([1], [[1,2]]) True >>> tree_embeds([1,[2,3]], [[True, False], 'ab']) True >>> tree_embeds([1,[2,3]], [[True, False], 'ab'], leaves=(str,)) False >>> tree_embeds([], [1,2,3]) False >>> tree_embeds(1, [1,2,3]) True
Map a function over the leaves of a tree. >>> tree_map(str, [1,[2,3]]) ['1', ['2', '3']] >>> tree_map(len, ['foo', ['x', 'asdf']]) [[1, 1, 1], [[1], [1, 1, 1, 1]]] >>> tree_map(len, ['foo', ['x', 'asdf']], leaves=(str,)) [3, [1, 4]]
The shape of a tree expressed as nested tuples of nothing. >>> tree_shape([1,[2,3]]) == tree_shape([True, 'ab']) True >>> tree_shape([1,[2,3]]) ((), ((), ())) >>> tree_shape(['ab', ['c', 'd']]) (((), ()), (((),), ((),))) >>> tree_shape(['ab', ['c', 'd']], leaves=(str,)) ((), ((), ())) >>> tree_shape(1) ()
Zip recursively. Returns nested lists with tuples at the bottom. >>> tree_zip([1,[2,3]], [5,[6,7]]) [(1, 5), [(2, 6), (3, 7)]] >>> tree_zip([1,[2,3]], [5,[6,7]], ['a',['b','c']]) [(1, 5, 'a'), [(2, 6, 'b'), (3, 7, 'c')]] >>> tree_zip('foo', 'bar') [('f', 'b'), ('o', 'a'), ('o', 'r')] >>> tree_zip(['foo'], ['bar']) [[('f', 'b'), ('o', 'a'), ('o', 'r')]] >>> tree_zip(['foo'], ['bar'], leaves=(str,)) [('foo', 'bar')] >>> tree_zip('foo', ['bar']) [('f', 'bar')] >>> tree_zip('foo', ['bar'], leaves=(str,)) ('foo', ['bar']) >>> tree_zip(1, 2) (1, 2) >>> tree_zip([1,[2,3]], [5,6,7,8], [['a','b'],['c','d']]) [(1, 5, ['a', 'b']), ([2, 3], 6, ['c', 'd'])]