Using the Python built-in Defaultdict method, you can easily 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 sub-dictionary:
Users = tree () users[' Harold ' [' username '] = ' HRLDCPR ' users[' handler ' [' username '] = ' matthandlersux '
print(json.dumps(users))
the JSON data can then be printed with the following results:
{"Harold": {"username": "HRLDCPR"}, "handler": {"username": "Matthandlersux"}}
No need to assign a value
We do not need to assign a value 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 a good message, you need to turn it into a standard Dictionary object:
def dicts (t): return {k:dicts (t[k]) for K in T}
You can now print 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 sub-structure is also treated as a Dictionary object, and the leaf node is an empty Dictionary object
Iteration
You can use interesting methods to iterate over a tree.
For example, if we parse a list of animals and add them to the previously defined taxonomy, we can use the following code:
Add (taxonomy, ' Animalia,chordata,mammalia,cetacea,balaenopteridae,balaenoptera,blue Whale '. Split (', '))
Simplified implementations:
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 potato ': {}}}, ' Solanaceae ' : {' Solanum ': {' potato ': {}, ' tomato ': {}}}}}
Conclusion
The above mentioned may be of little use, just some interesting code.
If you like Python, think of it as fun to understand.