Use of dictionaries in Python

Source: Internet
Author: User
Tags dota

The characteristics of a dictionary in Python:

The elements in the dictionary are hash-stored, without order,

Use the keyword (key) that is required in the dictionary when you are checking for additional deletions.

One: Create a dictionary

1) define directly one:

Dict = {' Ob1 ': ' Computer ', ' ob2 ': ' Mouse ', ' ob3 ': ' Printer '}

Note:

The dictionary can contain lists: dict={' yangrong ': [' ['] ', ' IT '], ' xiaohei ': ['] ', ' DotA '}

Dictionary can contain dictionary: dict={' yangrong ': {"Age": "" "," "Job": "IT"}, "Xiaohei": {"' Age ': ' '", ' job ': ' DotA '}}

2) Use Dict to generate one:

>>> name = (' Zhang ', ' lii ', ' ('), ' (' Yubo ', '), (' Zhangyou ', 9))
>>> dic = dict (name)
>>> DIC
{' zhangyou ': 9, ' lii ': +, ' Zhang ':, ' Yuybo ': 92}

3) Use the Zip method to merge two equal-length lists into one dict:

>>> a = ' www.baidu.com.cn.123 '
>>> a_list = A.split ('. ')
>>> a_list
[' www ', ' Baidu ', ' com ', ' cn ', ' 123 ']
>>> b_list = [' 163 ', ' com ', ' vedio ', ' oa ', ' B ']
>>> AA = dict (Zip (a_list,b_list))
>>> AA
{' Baidu ': ' com ', ' www ': ' 163 ', ' 123 ': ' B ', ' com ': ' vedio ', ' cn ': ' OA '}

The a_list list is a keyword straight in the new dictionary, while the B_list list is straight in the new dictionary.

2. Get

3. References and copies

4. Insert (update), delete

CC = {' QQ ': ' qq.com '}
>>> cc
{' QQ ': ' qq.com '}
>>> aa.update (CC)
>>> AA
{' Baidu ': ' com ', ' QQ ': ' qq.com ', ' www ': ' 163 ', ' 123 ': ' B ', ' CN ': ' OA ', ' com ': ' Vedio '}

5. Sorting

Reference: http://www.centoscn.com/python/2014/0318/2598.html

Use of dictionaries in Python

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.