Python Mapping--Dictionary

Source: Internet
Author: User

Python mapping--Introduction to dictionary Dictionaries

Dictionaries, as a kind of Python container, do not need to be ordered, so dictionaries are not part of the sequence. In this dictionary there are key (keys) and values (value). The value is stored in the key, so we can derive the value as long as we access the key. For the basic operation of the dictionary has one of the following, the most basic is the creation of the dictionary assignment, Access, update, delete .
1. There are three ways to create a dictionary, the first of which is the most common assignment, which is to assign a dictionary directly to a variable, then this variable is a dictionary. The second is to use the Dict () function, which translates the paired sequence into a dictionary, which is explained in detail below. The third is also the use of functions, but this function is not much to see, the way is: {}.fromheys (). The above three specific methods of use are as follows

#创建1dict1 = {' name ': ' Earth ', ' Port ': 80}print dict1------------->{' name ': ' Earth ', ' Port ': $ #创建2dict2 = Dict ([' X ', 1], [' Y ', 2]) print dict2------------->{' y ': 2, ' X ': 1} #创建3 #fromkeys receive the same value passed to the preceding key dict3 = {}.fromkeys ((' x ', ' Y '), 1) Print dict3------------->{' y ':-1, ' X ':-1}

Access to the dictionary also has two cases is worth saying, one is the normal access to the dictionary of an element, as long as the key in the [], and the other is in the case of traversing the dictionary: The default is to traverse the dictionary dictionary keys , rather than values, this is a more interesting place, There is no need to say anything, directly to the key to traverse, if this time requires the key corresponding to the value, just need to use the key to get the value can be

Dict1 = {' name ': ' Earth ', ' Port ': 80}for key in Dict1:    

Update, delete and sequence is not very big difference, in the sequence [] fill in the subscript, and in the dictionary, the value needs to change the subscript to the dictionary key. This also means that the dictionary is a container with no order.

Dict1 = {' name ': ' Earth ', ' Port ': 80}dict1[' name '] = ' sunos5 ' Print dict1------------->{' name ': ' sunos5 ', ' Port ': 80} Dict1 = {' name ': ' Earth ', ' Port ': 80}del dict1[' name '] #删除键为 ' name ' entry dict1.clear ()     #删除dict1中的所有条目print dict1.pop (' Name ') #删除 ' name ' entry and return the value of the key ' name '

Built-in functions

Built-in functions we explain 2 functions and a comparison size.

Dict ()

The Dict () function is used to create a dictionary and accepts only one parameter, so if the parameters inside are greater than 1, enclose them in parentheses. There are 3 parameters passed in, one is the null value, but the object can be iterated, and three is passed into a pair of dictionaries (this parameter does not need to be limited to one)

#传入可迭代对象时第一个是键, the second one is the value Dict4 = Dict (([' X ', 1], [' Y ', 2])) print dict4------------->{' y ': 2, ' x ': 1}dict5 = Dict ((' X ', ' Y '), (1, 2)) print dict5------------->{' y ': 2, ' X ': 1} #传入字典dict6 = Dict (x=1, y=2) print dict6------------->{' y ': 2, ' X ': 1}
Len ()

The Len () function is so flexible that it can be seen in many places, numbers, sequences, and dictionaries with its shadow, and when used in a dictionary, it returns the number of key pairs in the dictionary.

Dict1 = {' name ': ' Earth ', ' Port ': 80}print len (dict1)------------->2

Compare size

In fact, the dictionary comparison of the size of the opportunity is not many, the book is said to belong to advanced usage, so here only the comparison of the general method and order, no longer distance description:
First ratio: the length of the dictionary, the longer the dictionary, the larger.
More than: Dictionary keys, the same length of the case, the larger the key, the larger the dictionary.
Last comparison: The value of the dictionary, in the case of the same length and key, the larger the dictionary the larger the value.

Built-in methods

Dictionary built-in methods do not want to write more here, "Basic python Tutorial" in the Dictionary of the built-in functions are described in detail, there is a chance, want to put all the above functions are illustrated once.

Two attributes of a dictionary key

It says that the value in the dictionary is stored in the key, like a bed in a college dorm, and the value is the person who lives on the bed. Because of the need to live, the bed has a certain requirements, but there is no need to live on the bed of people. There are two specific requirements for beds (keys): 1. One bed is not allowed to sleep on the individual; 2. The bed must be lying flat-the key must be hashed.
1. One key is not allowed to correspond to multiple values

Each key corresponds to a value that does not allow multiple values to bind to a key, and if there is a conflict, take the last assignment

2. The key must be a hash

All immutable are hashed, and there are two issues to be aware of. Numbers do keys, and if the numbers are equal, the keys are the same; if the tuple does the key, the elements inside the tuple must all be immutable.

Python Mapping--Dictionary

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.