-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlevel.py
30 lines (21 loc) · 860 Bytes
/
level.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# level(lst,lspec) gives a list of all sublists of lst on levels specified by lspec.
# lspec specifications:
# n : levels 1 through n
# 0 : levels 1 through infinity
# [n] : levels n only
# [n1,n2] : levels n1 through n2
# level(lst,[-1]) gives a list of all terminal nodes in lst.
# A positive level n consists of all parts of lst specified by n indices.
# A negative level -n consists of all parts of lst with depth n.
# >>> level(['a',['b',['c','d']]],[-1])
# ['a', 'b', 'c', 'd']
# >>> level(['a',['b',['c','d']]],[2])
# ['b', ['c', 'd']]
# >>> level(['a',['b',['c','d']]],2)
# ['a', 'b', ['c', 'd'], ['b', ['c', 'd']]]
# >>> level(['a',['b',['c','d']]],0)
# ['a', 'b', 'c', 'd', ['c', 'd'], ['b', ['c', 'd']]]
from .part import part
from .indices import indices
def level(lst,lspec=[1]):
return [part(lst,*i) for i in indices(lst,lspec)]