Python advanced 01 dictionary

Source: Internet
Author: User

Author: vamei Source: http://www.cnblogs.com/vamei welcome reprint, please also keep this statement. Thank you!

 

Through our basic tutorial, we have established basic concepts for python and have a relatively clear understanding of objects and classes.

Our advanced tutorial is to further expand the basic tutorial and learn more about Python. I hope that you will have a comprehensive understanding of the basic syntax of python after the advanced tutorial.

 

As we mentioned earlier, a table is a class in Python. A specific table, such as NL = [1, 3, 8], is an object of this class. We can call some methods of this object, such as NL. append (15 ).
Now, we want to introduce a new class, that isDictionary). Similar to a table, a dictionary can also be used.Store Multiple Elements. The object that can be used to store multiple elements is collectively referred toContainer).

 

1. Basic Concepts

Common ways to create a dictionary:

>>> DIC = {'Tom ': 11, 'Sam': 57, 'lily ': 100}

>>> Print type (DIC)

Similar to a table, a dictionary containsMultiple Elements, Each element is separated by a comma. However, the dictionary has two elements,KeyAndValueGenerally, keys are represented by strings, and keys can also be represented by numbers or true values (immutable objects can be used as keys ). The value can be any object. Keys and values correspond to each other.

(In fact, the table element can also be any object)

For example, in the above example, 'Tom 'corresponds to 11, 'Sam corresponds to 57, and 'lily' corresponds to 100

 

Different from a table, the dictionary ElementNo order. YouElement cannot be referenced by subscript. Dictionary isReference by key.

>>> Print DIC ['Tom ']

>>> DIC ['Tom '] = 30

>>> Print DIC

 

You can create a new empty dictionary:

>>> DIC = {}

>>> Print DIC

 

Add a new element to the dictionary:

>>> DIC ['lilei'] = 99

>>> Print DIC

(Reference a new key and assign the corresponding value)

 

2. Call the dictionary element cyclically:

 
Dic = {'Lilei': 90,'Lily'100,'Sam': 57,'Tom': 90}ForKeyInDic:PrintDic [Key]

 

As you can see,In a loop, a key of dict is extracted and assigned to the key variable.

With the print result, we can confirm again that the elements in DIC are unordered.

 

3. Other common dictionary Methods

>>> Print DIC. Keys () # Return All DIC keys

>>> Print DIC. Values () # Return All DIC values

>>> Print DIC. Items () # Return All DIC elements (key-value pairs)

>>> DIC. Clear () # Clear DIC And change dict {}

 

There is also a common usage:

>>> Del DIC ['Tom '] # Delete the 'Tom' element of DIC.

DelIs a reserved keyword in Python, used to delete objects.

 

Similar to a table, you can useLen ()Queries the total number of elements in a dictionary.

>>> Print (LEN (DIC ))

 

Summary:

Each element in the dictionary is a key-value pair. The element has no order.

Dic = {'Tom ': 11, 'Sam': 57, 'lily ': 100}

Dic ['Tom '] = 99

For key in DIC :...

Del, Len ()

 

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.