The dictionary is the only built-in mapping type in Python. (Mappings are data structures that refer to values by name).
The values in the dictionary are not in a special order, but are stored in a specific key. Keys can be numbers, strings, and tuples.
First, create a dictionary:
1. Create a dictionary:
Such as:
phonebook={' Zhangsan ': ' 233333 ', ' Lisi ': ' 322233 ', ' Wangwu ': ' 4323334 '}
A dictionary consists of pairs of keys and their corresponding values (also referred to as key/value pairs). In the phonebook example, the name is the key and the phone number is the value. The keys and values are separated by a colon (:), and the items are separated by commas (,). The entire dictionary is enclosed by a pair of curly braces. An empty dictionary (excluding any items) consists of two curly braces, such as {}.
2. The Dict function creates a dictionary:
2.1. The Dict function establishes a dictionary through other mappings (such as other dictionaries) or sequence pairs (keys, values).
Such as:
>>> items=[(' name ', ' Zhangsan '), (' age ', 44)]
>>> d=dict (items)
>>> D
{' Age ': *, ' name ': ' Zhangsan '}
>>>
2.2, Dict function can also be used to create a dictionary by keyword parameters;
Such as:
>>> a=dict (name= ' Lisi ', age=44)
>>> D
{' Age ': *, ' name ': ' Zhangsan '}
>>>
Note: The Dict function is not a real function, he is a type, just like list, tuple, and Str.
Second, the basic operation of the dictionary:
The basic behavior of the dictionary is similar to the sequence (sequence):
A), Len (d): Returns the number of items in D (key-value pairs);
b), D[k]: Returns the value associated to the key k;
c), D[k]=v: The value V is associated to the key k;
d), Del D[k]: Delete the key to K;
e), k in D check if there is a value in D containing the key k;
Dictionaries and Lists
Python Data structure-Dictionary of sequences