Python basic knowledge 2: dictionary and python basic knowledge dictionary
A dictionary is a key-value data type, just like a dictionary used in school to search for Chinese characters through pinyin. A dictionary is the only ing type in Python. The dictionary object is variable. It is a container type that can store any number of Python objects, including other container types.
In a ing object, the hash value (key, key) has a one-to-many relationship with the object (value, value), which is generally considered a variable hash table.
1) create a dictionary:
Adict = {}
Adict = {key1: value2, key2: value2 ,...}
Dictionary features:
Features:
1. separate keys and values with the colon;
2. Separate items with commas;
3. The keys and keys in the dictionary must be unique, but the values may not be unique. They have a natural deduplication function.
4. The dictionary is unordered.
# Case 1:
Dict1 = {'a1': ['value: 1', 1, 21],
'A2 ': ['value: 2',],
'A3 ': ['value: 3', 3,23],
}
Print ("dict1", dict1)
Dict2 = {"a": [1, 2], "B": {"c": "d" }}# the value can be any value.
# Case 2
Print ("dict2", dict2)
Print ('dict2 element: ', dict2 ["a"] [1])
Execution result:
2) Common method: Dictionary
The Python dictionary contains the following built-in functions:
1. cmp (dict1, dict2): Compares two dictionary elements.
2. len (dict): calculates the number of dictionary elements, that is, the total number of keys.
3. str (dict): Output dictionary printable string representation.
4. type (variable): return the input variable type. If the variable is a dictionary, the dictionary type is returned.
The Python dictionary contains the following built-in methods:
1. radiansdict. clear (): deletes all elements in the dictionary.
2. radiansdict. copy (): returns the shortest copy of a dictionary.
3. radiansdict. fromkeys (): Creates a new dictionary and uses the elements in sequence seq as the dictionary key. val is the initial value corresponding to all keys in the dictionary.
4. radiansdict. get (key, default = None): return the value of the specified key. If the value is not in the dictionary, the default value is returned.
5. radiansdict. has_key (key): If the key is returned in the dictionary dict, false is returned.
6. radiansdict. items (): returns an array of (Key, value) tuples that can be traversed in a list.
7. radiansdict. keys (): returns all keys of a dictionary in a list.
8. radiansdict. setdefault (key, default = None): similar to get (). However, if the key does not exist in the dictionary, a key is added and the value is set to default.
9. radiansdict. update (dict2): update the dictionary dict2 key/value pair to dict.
10. radiansdict. values (): returns all values in the dictionary in a list.
Case 1: Add and modify elements
# Print ('----------- add element ----------------')
Dict1 = {'a1': ['value: 1', 1, 21],
'A2 ': ['value: 2',],
'A3 ': ['value: 3', 3,23],
}
Print ("dict1", dict1)
# Add a value
Dict1 ['a4 '] = ['value 4', 4, 24]
Print ("after dict added a4", dict1)
# Modify Value
Dict1 ['a4 '] [0] = [' value 42 ']
Print ("dict modify a4", dict1)
Execution result:Case 2: Delete
# Print ('----------- delete an element ----------------')
# Pop delete an element
Dict1.pop ("a4 ")
Print ("pop Delete element", dict1)
# Del delete an element
Del dict1 ["a3"]
Print ("del Delete element", dict1)
# Clear all items or elements in the dictionary;
Dict1.clear ()
Print ("clear Delete element", dict1)
Del dict1
Print ("del Delete dict1", dict1)
Execution result:
Case 3: three methods for finding or determining whether an exists
Case 2: three methods for finding or determining whether an exists
# Print ('----------- find the existing method ----------------')
Info = {'a1': ['value: 1', 1, 21],
'A2 ': ['value: 2',],
'A3 ': ['value: 3', 3,23],
'A4 ': ['value: 4',]
}
#1) method 1, using in to judge, there is also not in
T = 'a4 'in info
Print ('in result ', t)
#2) method 2, Use get to judge
T2 = info. get ('a4 ')
Print ('get method, return value exists ', t2)
# If the get value does not exist, none is returned.
T3 = info. get ('a5 ')
Print ('get method, return value exists ', t3)
# Method 3: direct reference. If the key exists, a value is returned. If the key does not exist, an error is returned.
T4 = info ['a4 ']
Print ('reference directly ', t4)
T5 = info ['a5 ']
Print ('direct reference, but the value does not exist', t5) # No returned results, error
Execution result:
Case 3: items, key, value
Info = {'a1': ['value: 1', 1, 21],
'A2 ': ['value: 2',],
'A3 ': ['value: 3', 3,23],
'A4 ': ['value: 4',]
}
# Traverse the dictionary key (key)
Print ("the key used to traverse the dictionary :")
For key in info. keys ():
Print (key)
# Traverse the dictionary value (value)
Print ("value for dictionary traversal :")
For I in info. values ():
Print (I)
# Traverse the dictionary and return a ancestor
Print ("dictionary traversal items :")
For I in info. items ():
Print (I)
# Traverse the dictionary's key-value
Print ("key-value for dictionary traversal :")
For I, j in info. items ():
Print (I, j)
Execution result:
Case 4: sort the dictionary and delete some key values
Deleting the preceding content is not the dictionary key.
Program:
Program link:Password for https://pan.baidu.com/s/1pLQfyRp: xjcv
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.