tutorial on using Python to demonstrate digital data structures

Source: Internet
Author: User
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.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.