Dictionaries are an unordered and variable sequence of key-value pairs in which each element is a key-value pair and contains: key objects and value objects. You can quickly get, delete and update the corresponding Value Object through the Key Object.
The corresponding Value Object is found in the dictionary through the Key Object. Keys are arbitrary, immutable data, such as integers, floating-point numbers, strings, tuples. However: Lists, dictionaries, collections, these mutable objects cannot be used as "keys". And the key is not repeatable. Values can be arbitrary and repeatable.
Catalog
1. Create dictionary objects from {}, dict()
(2) Create dictionary objects from zip
(3) Create a dictionary object with a value of None through fromkeys
3. Addition, modification and deletion of dictionaries
1. Creation of Dictionaries
1. Create dictionary objects from {}, dict()
a={key1:value1,key2:value2} a=dict([(key1,value1),(key2,value2)]) a=dict(key1=value1,key2=value2) #It looks like key1 and key2 default to strings, cannot be'', cannot start with a number
(2) Create dictionary objects from zip
a=dict(zip(key A list, tuple, or collection of,value A list, tuple, or collection of))
(3) Create a dictionary object with a value of None through fromkeys
a=dict.fromkeys(key A list, tuple, or collection of)
2. Access to Dictionaries
1 Access through []
adopt a[key]To access the corresponding value,Throw an exception if nonexistent
(2) Access through get()
get() method to get a value that returns None if the specified key does not exist, or set the default returned object if the specified key does not exist
a.get(key) #There is no default return Nonea.get(key, return content)
(3) List all key-value pairs
a=dict.fromkeys({1,2,3,4}) print(a.items()) #The result is: dict_items([(1, None), (2, None), (3, None), (4, None)])
(4) List all keys, all values
Key
a=dict.fromkeys({1,2,3,4})print(a.keys()) #The result is: dict_keys([1, 2, 3, 4])
Value
a=dict.fromkeys({1,2,3,4})print(a.values()) #The result is: dict_values([None, None, None, None])
⑤len()
Returns the key-value logarithm of a dictionary
a=dict.fromkeys({1,2,3,4})print(len(a)) #The result is:4
⑥ in
Determine if the key is in the dictionary
a=dict.fromkeys({1,2,3,4}) print(1 in a) #The result is:True
3. Addition, modification and deletion of dictionaries
Direct modification
If the key does not exist, a new key:value will be generated in the dictionary; Existing overrides original value
②update()
Add all key values from the new dictionary to the old dictionary object and override them directly if the key is duplicated
a={1:2,2:3,3:4} b={1:3,4:5} a.update(b) print(a) #The results are: {1:3, 2:3, 3:4, 4:5}
③del(),pop(),clear()
a={1:2,2:3,3:4}del(a[1]) #Delete key-value pair 1:2 print(a) a.pop(2) #Delete key value to 2:3 print(a) a.clear() #Delete all key-value pairs print(a) ''' The results are: {2: 3, 3: 4} {3: 4} {} '''
④popitem()
* Randomly return and delete the last pair of keys and values in the dictionary
a={1:2,2:3,3:4} b=a.popitem() print(b) print(a) b=a.popitem() print(b) print(a) ''' The results are: (3, 4) {1: 2, 2: 3} (2, 3) {1: 2} '''
4. Unpacking
Key operations
a={1:2,2:3,3:4} q,w,e=a print(q) print(w) print(e) ''' The results are: 1 2 3 '''
Key Value Operations
a={1:2,2:3,3:4} q,w,e=a.items() print(q) print(w) print(e) ''' The results are: (1, 2) (2, 3) (3, 4) '''
Value Operations
a={1:2,2:3,3:4} q,w,e=a.values() print(q) print(w) print(e) ''' The results are: 2 3 4 '''
5. Core Bottom Principles
1 Storage
At the heart of dictionary objects is hash lists. A Hash list is a sparse array (an array with always blank elements), and each cell of the array is called a bucket. Each bucket has two parts: a reference to a key object and a reference to a value object. Because all buckets have the same structure and size, we can read the specified bucket by an offset.
The hash of the key object is calculated first, then the binary number is converted to the decimal number based on the length of the array from right to left. If the index bucket is empty, the key-value pair is stored. If not, the binary number to the right of the hash value is converted to the decimal number in turn until the corresponding index bucket is empty.
Python will create a larger array based on the congestion level of the Hash list (close to two-thirds) and copy the original content into the new array. (
Reading
Consistent with the underlying stored process algorithm, it is also a number that takes hash values at different locations in turn. Converts a binary number from right to left to a decimal number based on the length of the array to find if the corresponding bucket is empty. If empty, returns None. If not null, calculate the corresponding hash value for this bucket's key object and compare it to our hash value, if equal. Returns the corresponding Value Object. If not, recalculate the offset by taking a few other digits in turn. They were taken one by one and still not found. None is returned.
3. Summary
1. Keys must be hashable
(1) Numbers, strings, tuples are all hashable.
(2) Custom objects need to support the following three points:
(1) Support hash() function
Support Passed u eq_u () Method to detect equality
(3) If a==b is true, hash(a)==hash(b) is also true
2. Dictionaries are expensive in memory and typically exchange time
3. Key queries are fast
4. Adding new entries to the dictionary may result in expansion and change the order of keys in the hash list. Therefore, do not modify the dictionary while traversing it (you can traverse a copy of the dictionary)