Using Python's built-in Defaultdict method makes it easy to define a tree's data structure.
Simply put, a tree can also be a dictionary data structure
def tree (): Return defaultdict (tree)
That's all, just one line of code.
If you continue with the following code, you need to first introduce
From collections Import Defaultdict
Instance
Json-esque
Now we create a json-esque nested dictionary without explicitly creating a child dictionary:
Users = tree ()
users[' Harold ' [' username '] = ' HRLDCPR ' users[' handler ']
[' username '] = ' matthandlersux '
The JSON data can then be printed through the <code>print (json.dumps) </code>, with the following results:
{"Harold": {"username": "HRLDCPR"}, "handler": {"username": "Matthandlersux"}}
No need to assign a value
We do not need to assign values to create a structure:
Taxonomy = tree ()
taxonomy[' Animalia ' [' Chordata '] [' Mammalia '] [' Carnivora '] [' Felidae '] [' felis '] [' cat ']
taxonomy[' Animalia ' [' Chordata '] [' Mammalia '] [' Carnivora '] [' Felidae '] [' Panthera '] [' lion ']
taxonomy[' Animalia '] [' chordata '] [' Mammalia '] [' Carnivora '] [' Canidae '] [' canis '] [' dog '] taxonomy[' Animalia '] [' chordata ']
[' Mammalia '] [' Carnivora '] [' Canidae '] [' canis '] [' Coyote '] taxonomy[' Plantae '] [' solanales '] [' Solanaceae '] [']
Solanum '] [' tomato ']
taxonomy[' Plantae ' [' Solanales ']] [' Solanaceae '] [' Solanum '] [' potato ']
taxonomy[' Plantae '] [' solanales '] [' Convolvulaceae '] [' Ipomoea '] [' Sweet potato ']
To print good information, you need to turn to a standard Dictionary object:
def dicts (t): return {k:dicts (t[k]) to K in T}
It is now available through Pprint (dicts (taxonomy)):
{' Animalia ': {' chordata ': {' Mammalia ': {' Carnivora ': {' Canidae ': {' canis ': {' coyote ': {},
' dog ': {}}},
' Felidae ': {' Felis ': {' cat ': {}},
' Panthera ': {' lion ': {}}}}},
' plantae ': {' solanales ': {' Convolvulaceae ': {' Ipomoea ': {' sweet potato ': {}}},
' Solanaceae ': {' Solanum ': {' potato ': {},
' tomato ': {}}}}
The child structure is also treated as a Dictionary object, and the leaf node is an empty Dictionary object
Iterations
You can use interesting methods to iterate the tree.
For example, if we parse an animal list and add it to the previously defined taxonomy, we can use the following code:
Add (taxonomy,
' Animalia,chordata,mammalia,cetacea,balaenopteridae,balaenoptera,blue Whale '. Split (', '))
Simplify implementation:
def add (t, keys): For
key in keys:
t = T[key]
We still don't need to assign a value:
{' Animalia ': {' chordata ': {' Mammalia ': {' Carnivora ': {' Canidae ': {' canis ': {' coyote ': {},
' dog ': {}}},
' Felidae ': {' Felis ': {' cat ': {}},
' Panthera ': {' lion ': {}}},
' Cetacea ': {' Balaenopteridae ': {' Balaenoptera ': { ' Blue Whale ': {}}}} {'
plantae ': {' solanales ': {' Convolvulaceae ': {' Ipomoea ': {' sweet ': {}}}, '
Solanaceae ': {' Solanum ': {' potato ': {},
' tomato ': {}}}}
Conclusion
The above mentioned may not be very useful, just do some interesting code.
If you like Python, take this as a pleasure to understand.