Introduction to the Dictionary of [python]

Source: Internet
Author: User
Tags delete key shallow copy

The dictionary is the only type of mapping in python, its hash value (key Key) and the point to object (value) is a one-to-many relationship;

A dictionary is a mutable object, is a container type, can store any number, any type of mapping type, and is enclosed in curly braces ({key1:value, key2:value2, ...). });

The data types in the dictionary are Unordered.

first, Create a dictionary

1.1 Assignment Creation

Dict1 = {...}

1.2 Factory method-dict ()

Dict2 = dict ([' x ', 1], [' y ', 2]) # {' x ': 1, ' y ': 2}

1.3 Built-in Method-fromkeys (), creating a default dictionary

Dict3.fromkeys (keys,value=none)

Where Keys is a tuple type, or it can be a single element, but the resulting dictionary is a different key corresponding to the same value, and the default is none if the value is Supplied.

second, Access to the dictionary

1.1 For Loop traversal dictionary keys key

For key in Dict:

Print key,dict[key]

1.2 Square brackets []

dict[key]

1.3 Boolean operation-in or not in

Used to detect if a key exists in the Dictionary.

Key in Dict

Key not in Dict

1.4 Updating Dictionaries

Dict4 = {' a ': 1, ' b ': 2}

Add a data item or element: dict4[' c '] = 4

Modify a data item or element: dict4[' a '] = ' m '

Delete a data item or element:

Del Delete a single key-value pair: del dict4[' a ']

Dict4.clear () clears dictionary key-value pairs

Value = Dict4.pop (' a ' [, default]) removes the specified key value pair from the dictionary and returns the value corresponding to

1.5 dictionaries do not support stitching (' + ') and repeating (' * ') operations

third, the dictionary type operator

3.1 Dictionary key Lookup operator-[]

Used to get the value corresponding to the key in the dictionary, or to add or modify Key-value pairs in the dictionary

3.2-key member relation operator-in or not in

Key in Dict: generally returns a Boolean value that can be used in the IF statement structure as a condition for judging

3.3 Standard type functions

Type () factory method: returns the dictionary type ' <type ' dict ' > '

STR () Factory method: Returns the string form of a dictionary

CMP (): dictionary comparison, which follows the order, dictionary length, Dictionary key, Dictionary value

3.4 Mapping type-related functions

Dict ([container]) Factory function, If the container (container) class is provided, populate the dictionary with its entries, or create a subroutine, dict (x=1, y=3)

Len () built-in function that returns the number of all elements (key-value Pairs)

Hash () built-in function to determine whether an element can be used as a dictionary key

Sorted (dict) Built-in functions, sort dictionaries by dictionary key

  

3.5 Dictionary Built-in methods

Dict.clear () Delete all elements in a dictionary
Dict.copy () Returns a copy of the word shallow copy
Dict.fromkeys (seq, Val=none) Creates and returns a new dictionary, which is a dictionary of the elements in seq, and Val is the value (default is none if Val is not supplied)
Dict.get (key,default=none) Gets the Val of the dictionary key, if the dictionary does not contain a key, returns the default value of the set
Dict.has_key (key) Key exists return true, otherwise false
Dict.items () Returns a list of tuple-key-value pairs in the dictionary
Dict.pop (key[,default]) If key exists, Delete and return dict[key]; if key does not exist, the default is returned, and if default is not set, an exception is thrown
Dict.setdefault (key,defaukt=none) Return dict[key], but do not delete key and val; if key does not exist, add Dict[key]=default in the dictionary
Dict.update (dict2) Adds a dictionary dict2 key-value pair to the dictionary Dict
Dcit.keys () Returns a list containing the keys in the dictionary
Dict.values Returns a list containing the values in the dictionary

3.6 Dictionary Keys

One key is not allowed to correspond to multiple values, and the last (most Recent) assignment is taken when a key conflict occurs (the dictionary key is assigned repeatedly)

The key must be hashed, and the immutable type is hash-able

Introduction to the Dictionary of [python]

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.