Python Core Programming (second Edition)--Mappings and collection types

Source: Internet
Author: User
Tags string format

1. Dictionaries

A dictionary is the only type of mapping in the Python language. The hash value (key) and the object (value) pointed to in the mapping type object are a one-to-many relationship. A Dictionary object is mutable, which is a container type that can store any number of Python objects, including other container types. The difference between a dictionary type and a sequence type container class (list, tuple) is how the data is stored and accessed differently. The sequence type is indexed only by numeric type keys (in numeric order from the beginning of the sequence). A mapping type can be used to make keys with other object types, and the most common is to use a string to do the key (keys). Unlike the key for a sequence type, the key for the mapping type is directly, or indirectly, associated with the stored data value. But because in the mapping type, we no longer use the "serialized sort" key (keys), the data in the mapping type is unordered.

Core Note : What is a hash table? What is their relationship to the dictionary?

The sequence type is indexed with an ordered number key to store the data as an array. In general, the index value has nothing to do with the stored data. You can also store data in a different way: based on a correlation value, such as a string. A hash table is a data structure: it works according to what we ask. Each piece of data stored in a hash table, called a value, is stored according to a data item associated with it that is referred to as a key (key). The keys and values together are called "key-value pairs" (Key-value pairs). The algorithm of the hash table is to get the key, perform an operation called a hash function on the key, and, based on the result of the calculation, choose to store your value in an address of the data structure. The address at which any value is stored depends on its key. Because of this randomness, the values in the hash table are not sequential. What you have is an unordered set of data.

The ordered collection you can get can only be a collection of keys in a dictionary or a collection of values. Method the keys () or values () returns a list, which is sortable. You can also sort by using the items () method to get a list of tuples that contain key and value pairs. Because the dictionary itself is hashed, it is unordered. A hash table generally has good performance because it is fairly fast to query with keys.

The syntax format for a dictionary entry is the key: value. Also, multiple dictionary entries are included in the ({}). To create a dictionary, you only need to assign a dictionary to a variable, regardless of whether the dictionary contains elements.

From the Python version 2.2, you can use the Factory method Dict () to create a dictionary;

Starting with the Python 2.3 version, you can create a "default" dictionary with a convenient built-in method, Fromkeys (), and the elements in the dictionary have the same value (none by default if not given). such as: Ddict = {}.fromkeys (' x ', ' Y '),-1);

To traverse a dictionary (usually with a key), you only need to loop through its keys, ex:for key in Dict2.keys (); Starting with Python 2.2, you can use iterators to access class sequence objects, Ex:for key in Dict2 (Dict2 is a dictionary created);

To get the value of an element in the dictionary, you can use the dictionary key you are familiar with in brackets to get: Ex dict2[' name ';

The best way to check if there is a key in a dictionary is the in or not in operator.

There are several ways to make changes to a dictionary:

(1) Add a new data item or new element (that is, a key-value pair);

(2) Modification of an existing data item;

(3) or delete an existing data item.

1>>> dict2['name'] ='Venus' #Update an existing entry2>>> dict2['Port'] = 6969#Update an existing entry3>>> dict2['Arch'] ='sunos5'#Add new entry4>>>5>>>Print 'host% (name) s is running on port% (port) d'%Dict26Host Venus isRunning on port 6969
View Code

The print statement above shows another way to use the string format character (%) in the dictionary. Use the dictionary parameter to simplify the print statement, because you only need to use the name of the dictionary once, instead of using the tuple parameter when each element appears.

Python Core Programming (second Edition)--Mappings and collection types

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.