Skip to content

Instantly share code, notes, and snippets.

@jdunck
Forked from gulnara/gist:6100119
Last active December 20, 2015 08:29
Show Gist options
  • Save jdunck/6100645 to your computer and use it in GitHub Desktop.
Save jdunck/6100645 to your computer and use it in GitHub Desktop.
import pprint
def nested_tree(edges):
nested = {}
for from_, to in edges:
value = {}
if nested.get(from_):
nested[from_][to]={}
else:
nested[from_] = value
value[to]= {}
pprint.pprint(nested)
if __name__ == '__main__':
edges = [
['a', 'b'],
['a', 'f'],
['a', 'j'],
['b', 'c'],
['b', 'd'],
['d', 'e'],
['f', 'g'],
['g', 'h'],
['g', 'i'],
]
nested_tree(edges)
# nested = {
# 'a': {
# 'b': {
# 'c': { },
# 'd': {
# 'e': { }
# }
# },
# 'f': {
# 'g': {
# 'h': { },
# 'i': { },
# },
# },
# 'j': { },
# }
# }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment