Python intensive training notes (6)-keep the dictionary in order and enhance python Training

Source: Internet
Author: User

Python intensive training notes (6)-keep the dictionary in order and enhance python Training

The python dictionary is a very convenient data structure. With it, we can easily find his score, ranking, and other values based on the name (key ), instead of traversing the entire dataset.

Example: {'lil': [1,100], 'jar': [2, 98]...}

However, there are some problems in the process of using the dictionary, that is, the dictionary itself does not matter the order you enter

>>> D ={}>> d ['Lee '] = [1,100] >>> d ['Jane'] = [2, 98] >>> d ['Tom '] = [1,100] >>> d {'jar': [2, 98], 'lil': [], 'Tom ': [3, 96]}

You can use OrderedDict of the collections module to meet this requirement.

>>> From collections import OrderedDict >>> d = OrderedDict () >>> d ['lil'] = [1,100] >>> d ['jar'] = [2, 98] >>> d ['Tom '] = [1,100] >>> dOrderedDict ([('Lee', []), ('Jane ', [2, 98]), ('Tom ', [3, 96])

 

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.