Advanced python Tutorials: dictionaries, dictionaries, dict, and python advanced tutorials

Source: Internet
Author: User

Advanced python Tutorials: dictionaries, dictionaries, dict, and python advanced tutorials

The basic tutorial introduces basic concepts, especially objects and classes.

The advanced tutorial further expands the basic tutorial to illustrate the details of Python. I hope you will have a more comprehensive understanding of Python after the advanced tutorial.

As we mentioned earlier, the list 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 ).

We will introduce a new class, dictionary ). Similar to the List, a dictionary can store multiple elements. The object that stores multiple elements is called a container ).

Basic Concepts

Common ways to create a dictionary:
Copy codeThe Code is as follows:
>>> Dic = {'Tom ': 11, 'Sam': 57, 'lily ': 100}
>>> Print type (dic)

Similar to a table, a dictionary contains multiple elements separated by commas. However, the dictionary element contains two parts: keys and values. Commonly, keys are represented by strings. You can also use numbers or true values to represent keys (immutable objects can be used as keys ). The value can be any object. Keys and values correspond to each other.

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 elements have no order. You cannot reference an element by subscript. A dictionary is referenced by a key.
Copy codeThe Code is as follows:
>>> Print dic ['Tom ']
>>> Dic ['Tom '] = 30
>>> Print dic

Create a new empty dictionary:
Copy codeThe Code is as follows:
>>> Dic = {}
>>> Print dic

 

Add a new element to the dictionary:
Copy codeThe Code is as follows:
>>> Dic ['lilei'] = 99
>>> Print dic

Here, we reference a new key and assign it the corresponding value.

Cyclic call of dictionary elements
Copy codeThe Code is as follows:
Dic = {'lilei': 90, 'lily': 100, 'Sam ': 57, 'Tom': 90}
For key in dic:
Print dic [key]

In a loop, each 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.

Common dictionary Methods

Copy codeThe Code is as follows:
>>> 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:
Copy codeThe Code is as follows:
>>> Del dic ['Tom '] # Delete the 'Tom' element of dic.

Del is a reserved keyword in Python and is used to delete objects.

Similar to a table, you can use len () to query the total number of elements in the dictionary.
Copy codeThe Code is as follows:
>>> Print (len (dic ))

Summary

Each element in the dictionary is a key-value pair. The element has no order.
Copy codeThe Code is as follows:
Dic = {'Tom ': 11, 'Sam': 57, 'lily ': 100}
Dic ['Tom '] = 99
For key in dic :...
Del, len ()


In python, how does one convert list and dictionary dict into strings?

A = {'id': 1}
B = [1, 2]
C = str ()
D = str (B)
Print (c) => "{'id': 1 }"
Print (d) => "[1, 2]"

How does the list () function of python convert the dictionary into a list?

Positive Solution. Unless you do not need dict, there is an OrderedDict in python that stores the insertion sequence.
Yes.
Z = collections. OrderedDict ()
Z ['banji '] = 'sanban ',
Z ['X'] = 'honging ',
Z ['nianji'] = 'wunianji'
Then you want the result.

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.