FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
kite-python-blog-post-code/python-typing/tree.py at master · kiteco/kite-python-blog-post-code · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
kiteco
/
kite-python-blog-post-code
Public
Notifications
You must be signed in to change notification settings
Fork
159
Star
247
Code
Issues
1
Pull requests
43
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
kite-python-blog-post-code
/
python-typing
/
tree.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
49 lines (32 loc) · 1015 Bytes
Breadcrumbs
kite-python-blog-post-code
/
python-typing
/
tree.py
Copy path
File metadata and controls
49 lines (32 loc) · 1015 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# tree.py (final)
from
typing
import
Tuple
,
Iterable
,
Dict
,
List
,
DefaultDict
,
TypeVar
from
collections
import
defaultdict
T
=
TypeVar
(
'T'
)
Relation
=
Tuple
[
T
,
T
]
def
create_tree
(
tuples
:
Iterable
[
Relation
])
->
DefaultDict
[
T
,
List
[
T
]]:
"""
Return a tree given tuples of (child, father)
The tree structure is as follows:
tree = {node_1: [node_2, node_3],
node_2: [node_4, node_5, node_6],
node_6: [node_7, node_8]}
"""
# convert to dict
tree
:
DefaultDict
[
T
,
List
[
T
]]
=
defaultdict
(
list
)
for
pair
in
tuples
:
child
,
father
=
pair
if
father
:
tree
[
father
].
append
(
child
)
return
tree
print
(
create_tree
([(
2.0
,
1.0
), (
3.0
,
1.0
), (
4.0
,
3.0
), (
1.0
,
6.0
)]))
"""
Generic Classes
"""
def
return_values
()
->
Iterable
[
float
]:
yield
4.0
yield
5.0
yield
6.0
def
chain
(
*
args
:
Iterable
[
T
])
->
Iterable
[
T
]:
for
arg
in
args
:
yield
from
arg
print
(
list
(
chain
([
1
,
2
,
3
],
return_values
(),
'string'
)))
Back
|
FazBrowse Home
|
New Git URL