Python Review and Collation 5: images and collective types

Source: Internet
Author: User

0. Description


Still is to summarize the word image type and collection type according to the thought of several sequence types described earlier, that is to do an introduction first, then discuss the available operators, factory functions, built-in functions and methods.



1. Mapping Type: Dictionary


The mapping type is called a hash table, and the Dictionary object in Python is the hash type, that is, it stores the data in such a way that it is afetch key, an operation called a hash function on the key, and, based on the result of the calculation, a choice to store the value of the object in an address of the data structure. . Any value stored in the address depends on its key, and because of this randomness, the values in the hash table are not sequential, so the elements in the Dictionary object in Python are also unordered.

Here is the basic operation of the dictionary:

(1) Creating dictionaries and assigning values to dictionaries

The main methods are as follows:

    • Create assignments directly : can be assigned as a whole, or you can add key-value pairs individually

    • Factory function Dict ()

>>> fdict = Dict ([' Xpleaf ', ' Boy '], [' Cl ', ' Girl ']) >>> fdict{' xpleaf ': ' Boy ', ' cl ': ' Girl '}
    • built-in function Fromkeys (): Creates a default dictionary in which elements in the dictionary have the same value (none by default if not given)

>>> fdict = {}.fromkeys (' xpleaf ', ' cl '), ' lovings ') >>> fdict{' xpleaf ': ' Lovings ', ' cl ': ' Lovings '} >>> edict = {}.fromkeys (' xpleaf ', ' cl ') >>> edict{' xpleaf ': None, ' CL ': none}


(2) Accessing values in a dictionary

    • Key of the Loop dictionary

>>> fdict = {' xpleaf ': ' Boy ', ' cl ': ' Girl '}>>>-key in Fdict.keys (): ... print key, Fdict[key] ... xp Leaf Boycl Girl
    • Using iterators to access class sequence objects

>>> fdict = {' xpleaf ': ' Boy ', ' cl ': ' Girl '}>>>-key in Fdict: ... print key, Fdict[key] ... xpleaf bo YCL Girl
    • First determine if the key value exists and then access

>>> fdict = {' xpleaf ': ' Boy ', ' cl ': ' Girl '}>>> fdict.has_key (' xpleaf ') true>>> ' xpleaf ' in Fdicttrue>>> ' Clyyh ' in Fdictfalse


(3) Updating the dictionary

You can update existing key-value pairs or add new key-value pairs:

>>> fdict = {' host ': ' Xpleaf ', ' Port ': 80}>>> fdict[' port '] = 3389>>> fdict[' ip '] = ' 121.42.147 .131 ' >>> print ' Host:% (host) s, Port:% (port) s, IP:% (IP) s '% fdicthost:xpleaf, port:3389, ip:121.42.147.131


(4) Delete dictionary elements and dictionaries

>>> fdict = {' host ': ' Xpleaf ', ' Port ': ' IP ': ' 121.42.147.131 '}>>> del fdict[' Port '] #删除一个键值对 >> ;> fdict.clear () #删除字典中所有的键值对 >>> del fdict #删除整个字典



3. Mapping type Operators


In addition to not supporting stitching and repetition, dictionaries can work with all standard type operators.


(1) Standard type operator

Standard type operators have been introduced in the Python review and Collation 2:python objects, which are also suitable for dictionary types, mainly:>, <, = =, and and, as follows:

>>> dict1 = {' abc ': 123}>>> dict2 = {' abc ': 456}>>> dict1 < dict2true>>> Dict1 = = d Ict2false


(2) Mapping type operator

    • Key lookup operator for dictionary ([])

For a sequence type, an index is used to make a unique parameter or subscript to get the value of a sequence, but for a dictionary, it is a key query, and the key is a parameter, not an index. The ' [] ' operator allows you to find an element in a dictionary, or to assign a value to an element.

    • Key member relationship operation (in, not in)

Of course, you can also use Has_key ().



This article is from the "fragrant fluttering leaves" blog, please make sure to keep this source http://xpleaf.blog.51cto.com/9315560/1753670

Python Review and Collation 5: images and collective 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.