Skip to content

Instantly share code, notes, and snippets.

@ChuntaoLu
Forked from hrldcpr/tree.md
Created August 20, 2014 21:30

Revisions

  1. @hrldcpr hrldcpr revised this gist Aug 12, 2014. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions tree.md
    Original file line number Diff line number Diff line change
    @@ -31,7 +31,7 @@ We can print this as json with `print(json.dumps(users))` and we get the expecte
    ```

    ### Without assignment
    We can even create structure with no assignment at all:
    We can even create structure with no assignment at all, since merely referencing an entry creates it:

    ```python
    taxonomy = tree()
    @@ -76,9 +76,9 @@ add(taxonomy,
    We can implement this simply as:

    ```python
    def add(t, keys):
    for key in keys:
    t = t[key]
    def add(t, path):
    for node in path:
    t = t[node]
    ```

    Again we are never assigning to the dictionary, but just by referencing the keys we have created our new structure:
  2. @hrldcpr hrldcpr revised this gist Aug 12, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion tree.md
    Original file line number Diff line number Diff line change
    @@ -70,7 +70,7 @@ This tree can be fun to iteratively walk through, again because structure comes
    For example, suppose we are parsing a list of new animals to add to our taxonomy above, so we want to call a function like:
    ```python
    add(taxonomy,
    'Animalia,Chordata,Mammalia,Cetacea,Balaenopteridae,Balaenoptera,blue whale'.split(','))
    'Animalia>Chordata>Mammalia>Cetacea>Balaenopteridae>Balaenoptera>blue whale'.split('>'))
    ```

    We can implement this simply as:
  3. @hrldcpr hrldcpr revised this gist Jan 13, 2013. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion tree.md
    Original file line number Diff line number Diff line change
    @@ -97,4 +97,6 @@ Again we are never assigning to the dictionary, but just by referencing the keys
    ## Conclusion
    This probably isn't very useful, and it makes for some perplexing code (see `add()` above).

    But if you like Python then I hope this was fun to think about or worthwhile to understand.
    But if you like Python then I hope this was fun to think about or worthwhile to understand.

    There was a [good discussion](http://news.ycombinator.com/item?id=3881171) of this gist on Hacker News.
  4. @hrldcpr hrldcpr revised this gist Apr 23, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion tree.md
    Original file line number Diff line number Diff line change
    @@ -12,7 +12,7 @@ It simply says that a tree is a dict whose default values are trees.

    (If you're following along at home, make sure to `from collections import defaultdict`)

    Edit: Hacker News reader @zbuc points out that this is called [autovivification](https://en.wikipedia.org/wiki/Autovivification). Cool!
    (Also: Hacker News reader @zbuc points out that this is called [autovivification](https://en.wikipedia.org/wiki/Autovivification). Cool!)

    ## Examples
    ### JSON-esque
  5. @hrldcpr hrldcpr revised this gist Apr 23, 2012. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions tree.md
    Original file line number Diff line number Diff line change
    @@ -12,6 +12,7 @@ It simply says that a tree is a dict whose default values are trees.

    (If you're following along at home, make sure to `from collections import defaultdict`)

    Edit: Hacker News reader @zbuc points out that this is called [autovivification](https://en.wikipedia.org/wiki/Autovivification). Cool!

    ## Examples
    ### JSON-esque
  6. @hrldcpr hrldcpr revised this gist Apr 20, 2012. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions tree.md
    Original file line number Diff line number Diff line change
    @@ -49,8 +49,6 @@ We'll prettyprint this time, which requires us to convert to standard dicts firs
    def dicts(t): return {k: dicts(t[k]) for k in t}
    ```

    (Compare this to the definition of `tree()` above, as it is in some sense its inverse.)

    Now we can prettyprint the structure with `pprint(dicts(taxonomy))`:

    ```python
  7. @hrldcpr hrldcpr revised this gist Apr 20, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion tree.md
    Original file line number Diff line number Diff line change
    @@ -49,7 +49,7 @@ We'll prettyprint this time, which requires us to convert to standard dicts firs
    def dicts(t): return {k: dicts(t[k]) for k in t}
    ```

    (Compare this to the definition of `tree` above, as it is in some sense its inverse.)
    (Compare this to the definition of `tree()` above, as it is in some sense its inverse.)

    Now we can prettyprint the structure with `pprint(dicts(taxonomy))`:

  8. @hrldcpr hrldcpr revised this gist Apr 20, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion tree.md
    Original file line number Diff line number Diff line change
    @@ -49,7 +49,7 @@ We'll prettyprint this time, which requires us to convert to standard dicts firs
    def dicts(t): return {k: dicts(t[k]) for k in t}
    ```

    (Note how similar this is to the definition of `tree` above, as it is in some sense the "inverse".)
    (Compare this to the definition of `tree` above, as it is in some sense its inverse.)

    Now we can prettyprint the structure with `pprint(dicts(taxonomy))`:

  9. @hrldcpr hrldcpr revised this gist Apr 20, 2012. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion tree.md
    Original file line number Diff line number Diff line change
    @@ -49,7 +49,9 @@ We'll prettyprint this time, which requires us to convert to standard dicts firs
    def dicts(t): return {k: dicts(t[k]) for k in t}
    ```

    Now we can print the structure with `pprint(dicts(taxonomy))`:
    (Note how similar this is to the definition of `tree` above, as it is in some sense the "inverse".)

    Now we can prettyprint the structure with `pprint(dicts(taxonomy))`:

    ```python
    {'Animalia': {'Chordata': {'Mammalia': {'Carnivora': {'Canidae': {'Canis': {'coyote': {},
  10. @hrldcpr hrldcpr revised this gist Apr 20, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion tree.md
    Original file line number Diff line number Diff line change
    @@ -46,7 +46,7 @@ taxonomy['Plantae']['Solanales']['Convolvulaceae']['Ipomoea']['sweet potato']
    We'll prettyprint this time, which requires us to convert to standard dicts first:

    ```python
    def dicts(t): return {k: dicts(v) for k,v in t.items()}
    def dicts(t): return {k: dicts(t[k]) for k in t}
    ```

    Now we can print the structure with `pprint(dicts(taxonomy))`:
  11. @hrldcpr hrldcpr revised this gist Apr 20, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion tree.md
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,7 @@ def tree(): return defaultdict(tree)

    That's it!

    It simply says that a tree is a dict whose default values are trees. Think about it.
    It simply says that a tree is a dict whose default values are trees.

    (If you're following along at home, make sure to `from collections import defaultdict`)

  12. @hrldcpr hrldcpr revised this gist Apr 20, 2012. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion tree.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@
    ## One-line Tree in Python
    # One-line Tree in Python

    Using Python's built-in [defaultdict](http://docs.python.org/library/collections.html#collections.defaultdict) we can easily define a tree data structure:

    ```python
  13. @hrldcpr hrldcpr revised this gist Apr 20, 2012. 1 changed file with 1 addition and 3 deletions.
    4 changes: 1 addition & 3 deletions tree.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,4 @@
    # One-line Tree in Python

    ## Definition
    ## One-line Tree in Python
    Using Python's built-in [defaultdict](http://docs.python.org/library/collections.html#collections.defaultdict) we can easily define a tree data structure:

    ```python
  14. @hrldcpr hrldcpr revised this gist Apr 20, 2012. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions tree.md
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,6 @@
    # One-line Tree in Python

    ## Definition
    Using Python's built-in [defaultdict](http://docs.python.org/library/collections.html#collections.defaultdict) we can easily define a tree data structure:

    ```python
  15. @hrldcpr hrldcpr revised this gist Apr 20, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion tree.md
    Original file line number Diff line number Diff line change
    @@ -96,4 +96,4 @@ Again we are never assigning to the dictionary, but just by referencing the keys
    ## Conclusion
    This probably isn't very useful, and it makes for some perplexing code (see `add()` above).

    But if you like Python then it might be fun to think about and worthwhile to understand.
    But if you like Python then I hope this was fun to think about or worthwhile to understand.
  16. @hrldcpr hrldcpr revised this gist Apr 20, 2012. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions tree.md
    Original file line number Diff line number Diff line change
    @@ -13,8 +13,6 @@ It simply says that a tree is a dict whose default values are trees. Think about
    (If you're following along at home, make sure to `from collections import defaultdict`)




    ## Examples
    ### JSON-esque
    Now we can create JSON-esque nested dictionaries without explicitly creating sub-dictionaries—they magically come into existence as we reference them:
  17. @hrldcpr hrldcpr revised this gist Apr 20, 2012. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions tree.md
    Original file line number Diff line number Diff line change
    @@ -3,9 +3,7 @@
    Using Python's built-in [defaultdict](http://docs.python.org/library/collections.html#collections.defaultdict) we can easily define a tree data structure:

    ```python
    ####################################
    def tree(): return defaultdict(tree)
    ####################################
    ```

    That's it!
    @@ -15,6 +13,8 @@ It simply says that a tree is a dict whose default values are trees. Think about
    (If you're following along at home, make sure to `from collections import defaultdict`)




    ## Examples
    ### JSON-esque
    Now we can create JSON-esque nested dictionaries without explicitly creating sub-dictionaries—they magically come into existence as we reference them:
  18. @hrldcpr hrldcpr revised this gist Apr 20, 2012. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion tree.md
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,9 @@
    Using Python's built-in [defaultdict](http://docs.python.org/library/collections.html#collections.defaultdict) we can easily define a tree data structure:

    ```python
    ####################################
    def tree(): return defaultdict(tree)
    ####################################
    ```

    That's it!
    @@ -96,4 +98,4 @@ Again we are never assigning to the dictionary, but just by referencing the keys
    ## Conclusion
    This probably isn't very useful, and it makes for some perplexing code (see `add()` above).

    But if you like Python then it might be fun to think about and worthwhile to understand.
    But if you like Python then it might be fun to think about and worthwhile to understand.
  19. @hrldcpr hrldcpr revised this gist Apr 20, 2012. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion tree.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,5 @@
    # One-line Tree in Python

    ## Definition
    Using Python's built-in [defaultdict](http://docs.python.org/library/collections.html#collections.defaultdict) we can easily define a tree data structure:

    ```python
  20. @hrldcpr hrldcpr revised this gist Apr 20, 2012. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion tree.md
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,9 @@ Using Python's built-in [defaultdict](http://docs.python.org/library/collections
    def tree(): return defaultdict(tree)
    ```

    That's it! Think about it.
    That's it!

    It simply says that a tree is a dict whose default values are trees. Think about it.

    (If you're following along at home, make sure to `from collections import defaultdict`)

  21. @hrldcpr hrldcpr revised this gist Apr 20, 2012. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions tree.md
    Original file line number Diff line number Diff line change
    @@ -93,6 +93,6 @@ Again we are never assigning to the dictionary, but just by referencing the keys
    ```

    ## Conclusion
    This probably isn't very useful, and it makes for some perplexing code (see `add()` above),
    This probably isn't very useful, and it makes for some perplexing code (see `add()` above).

    but if you like Python then it might be fun to think about and worthwhile to understand.
    But if you like Python then it might be fun to think about and worthwhile to understand.
  22. @hrldcpr hrldcpr revised this gist Apr 20, 2012. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions tree.md
    Original file line number Diff line number Diff line change
    @@ -94,4 +94,5 @@ Again we are never assigning to the dictionary, but just by referencing the keys

    ## Conclusion
    This probably isn't very useful, and it makes for some perplexing code (see `add()` above),

    but if you like Python then it might be fun to think about and worthwhile to understand.
  23. @hrldcpr hrldcpr revised this gist Apr 20, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion tree.md
    Original file line number Diff line number Diff line change
    @@ -94,4 +94,4 @@ Again we are never assigning to the dictionary, but just by referencing the keys

    ## Conclusion
    This probably isn't very useful, and it makes for some perplexing code (see `add()` above),
    but if you like Python then hopefully it's fun to think about and interesting to understand.
    but if you like Python then it might be fun to think about and worthwhile to understand.
  24. @hrldcpr hrldcpr revised this gist Apr 20, 2012. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions tree.md
    Original file line number Diff line number Diff line change
    @@ -91,3 +91,7 @@ Again we are never assigning to the dictionary, but just by referencing the keys
    'Solanaceae': {'Solanum': {'potato': {},
    'tomato': {}}}}}}
    ```

    ## Conclusion
    This probably isn't very useful, and it makes for some perplexing code (see `add()` above),
    but if you like Python then hopefully it's fun to think about and interesting to understand.
  25. @hrldcpr hrldcpr revised this gist Apr 20, 2012. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions tree.md
    Original file line number Diff line number Diff line change
    @@ -67,14 +67,14 @@ This tree can be fun to iteratively walk through, again because structure comes

    For example, suppose we are parsing a list of new animals to add to our taxonomy above, so we want to call a function like:
    ```python
    insert(taxonomy,
    'Animalia,Chordata,Mammalia,Cetacea,Balaenopteridae,Balaenoptera,blue whale'.split(','))
    add(taxonomy,
    'Animalia,Chordata,Mammalia,Cetacea,Balaenopteridae,Balaenoptera,blue whale'.split(','))
    ```

    We can implement this simply as:

    ```python
    def insert(t, keys):
    def add(t, keys):
    for key in keys:
    t = t[key]
    ```
  26. @hrldcpr hrldcpr revised this gist Apr 20, 2012. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion tree.md
    Original file line number Diff line number Diff line change
    @@ -67,7 +67,8 @@ This tree can be fun to iteratively walk through, again because structure comes

    For example, suppose we are parsing a list of new animals to add to our taxonomy above, so we want to call a function like:
    ```python
    insert(taxonomy, 'Animalia,Chordata,Mammalia,Cetacea,Balaenopteridae,Balaenoptera,blue whale'.split(','))
    insert(taxonomy,
    'Animalia,Chordata,Mammalia,Cetacea,Balaenopteridae,Balaenoptera,blue whale'.split(','))
    ```

    We can implement this simply as:
  27. @hrldcpr hrldcpr revised this gist Apr 20, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion tree.md
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    # One-line Tree in Python

    ## Definition
    Using Python's built-in [defaultdict](http://docs.python.org/library/collections.html#collections.defaultdict) we can easily define a tree datastructure:
    Using Python's built-in [defaultdict](http://docs.python.org/library/collections.html#collections.defaultdict) we can easily define a tree data structure:

    ```python
    def tree(): return defaultdict(tree)
  28. @hrldcpr hrldcpr revised this gist Apr 20, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion tree.md
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    # One-line Tree in Python

    ## Definition
    Using Python's built-in [defaultdict](http://docs.python.org/library/collections.html#collections.defaultdict) we can easily define a tree structure:
    Using Python's built-in [defaultdict](http://docs.python.org/library/collections.html#collections.defaultdict) we can easily define a tree datastructure:

    ```python
    def tree(): return defaultdict(tree)
  29. @hrldcpr hrldcpr revised this gist Apr 20, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion tree.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    # One-line Tree Datastructure in Python
    # One-line Tree in Python

    ## Definition
    Using Python's built-in [defaultdict](http://docs.python.org/library/collections.html#collections.defaultdict) we can easily define a tree structure:
  30. @hrldcpr hrldcpr revised this gist Apr 20, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion tree.md
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    # One-line Tree Datastructure in Python

    ## Definition
    Using Python's built-in [defaultdict](http://docs.python.org/library/collections.html#collections.defaultdict) we can easily define a versatile tree structure:
    Using Python's built-in [defaultdict](http://docs.python.org/library/collections.html#collections.defaultdict) we can easily define a tree structure:

    ```python
    def tree(): return defaultdict(tree)