Revisions
-
hrldcpr revised this gist
Aug 12, 2014 . 1 changed file with 4 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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, 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, 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: -
hrldcpr revised this gist
Aug 12, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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('>')) ``` We can implement this simply as: -
hrldcpr revised this gist
Jan 13, 2013 . 1 changed file with 3 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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. There was a [good discussion](http://news.ycombinator.com/item?id=3881171) of this gist on Hacker News. -
hrldcpr revised this gist
Apr 23, 2012 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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`) (Also: Hacker News reader @zbuc points out that this is called [autovivification](https://en.wikipedia.org/wiki/Autovivification). Cool!) ## Examples ### JSON-esque -
hrldcpr revised this gist
Apr 23, 2012 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 -
hrldcpr revised this gist
Apr 20, 2012 . 1 changed file with 0 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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} ``` Now we can prettyprint the structure with `pprint(dicts(taxonomy))`: ```python -
hrldcpr revised this gist
Apr 20, 2012 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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.) Now we can prettyprint the structure with `pprint(dicts(taxonomy))`: -
hrldcpr revised this gist
Apr 20, 2012 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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.) Now we can prettyprint the structure with `pprint(dicts(taxonomy))`: -
hrldcpr revised this gist
Apr 20, 2012 . 1 changed file with 3 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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} ``` (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': {}, -
hrldcpr revised this gist
Apr 20, 2012 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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(t[k]) for k in t} ``` Now we can print the structure with `pprint(dicts(taxonomy))`: -
hrldcpr revised this gist
Apr 20, 2012 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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. (If you're following along at home, make sure to `from collections import defaultdict`) -
hrldcpr revised this gist
Apr 20, 2012 . 1 changed file with 2 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,5 @@ # 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 -
hrldcpr revised this gist
Apr 20, 2012 . 1 changed file with 1 addition and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,4 @@ ## 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 -
hrldcpr revised this gist
Apr 20, 2012 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 -
hrldcpr revised this gist
Apr 20, 2012 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 I hope this was fun to think about or worthwhile to understand. -
hrldcpr revised this gist
Apr 20, 2012 . 1 changed file with 0 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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: -
hrldcpr revised this gist
Apr 20, 2012 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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: -
hrldcpr revised this gist
Apr 20, 2012 . 1 changed file with 3 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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. -
hrldcpr revised this gist
Apr 20, 2012 . 1 changed file with 0 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,5 @@ # 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 -
hrldcpr revised this gist
Apr 20, 2012 . 1 changed file with 3 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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! 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`) -
hrldcpr revised this gist
Apr 20, 2012 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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). But if you like Python then it might be fun to think about and worthwhile to understand. -
hrldcpr revised this gist
Apr 20, 2012 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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. -
hrldcpr revised this gist
Apr 20, 2012 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 it might be fun to think about and worthwhile to understand. -
hrldcpr revised this gist
Apr 20, 2012 . 1 changed file with 4 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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. -
hrldcpr revised this gist
Apr 20, 2012 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 add(taxonomy, 'Animalia,Chordata,Mammalia,Cetacea,Balaenopteridae,Balaenoptera,blue whale'.split(',')) ``` We can implement this simply as: ```python def add(t, keys): for key in keys: t = t[key] ``` -
hrldcpr revised this gist
Apr 20, 2012 . 1 changed file with 2 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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(',')) ``` We can implement this simply as: -
hrldcpr revised this gist
Apr 20, 2012 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 data structure: ```python def tree(): return defaultdict(tree) -
hrldcpr revised this gist
Apr 20, 2012 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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: ```python def tree(): return defaultdict(tree) -
hrldcpr revised this gist
Apr 20, 2012 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ # 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: -
hrldcpr revised this gist
Apr 20, 2012 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 tree structure: ```python def tree(): return defaultdict(tree)
NewerOlder