1, Dictionary
Dictionary is a kind of mapping type, that is, key value combination. The elements in the dictionary are unordered. Keys can be strings, numbers or tuples, but lists cannot be used.
(1) Dictionary creation
Create a dictionary by using curly braces, or {}, which can contain multiple key value pairs (key value), with colon: connections between keys and values, and multiple key value pairs separated by commas.
If there are no key value pairs in curly braces, an empty dictionary will be created. If there are duplicate keys in the dictionary, the old key will be overwritten.
x = {"a": "first", "a": "second"} print(x)
It can be seen that the key of the dictionary has been replaced with "second":
For example, the following python code:
x = {"a": "first", "b": "second", "c": 1, "d": 2} y = {} print(x, y)
The operation results are as follows:
In addition, you can create a dictionary by using the built-in function dict() function, where key value pairs are connected by using = connection, that is, key = value.
For example, the following python code:
a = dict(name="xiaoming", sex="male", age=20) print(a)
The operation results are as follows:
If you create a dictionary by using a function, an error will be reported when there are duplicate keys in the dictionary.
(2) Dictionary access
Since the key in the dictionary is unique, you can access and obtain the value corresponding to the key through the key, such as the following python code:
a = dict(name="xiaoming", sex="male", age=20) print(a['name'])
The operation results are as follows:
If the key to be accessed does not exist in the dictionary, an error will be reported.
(3) Addition, deletion and modification of dictionary
1. Addition of dictionary
Add a dictionary element by specifying a new key or by using the update() function.
For example, the following python code specifies to create a new key tel with a value of 1234567; Use the update function to add a new element:
a = {"name": "xiaoming", "sex": "male", "age": 20} print(a) a["tel"] = 1234567 print(a) a.update(address="Rod 1") print(a)
The operation results are as follows:
2. Dictionary deletion
You can delete the key value of the specified element in the dictionary by using the pop() function.
Use the popitem() function to randomly delete the elements in the dictionary.
Use the clear() function to clear all elements in the dictionary.
For example, the following python code specifies to delete the age key value pair. The popitem() function randomly deletes the tel key value pair. Finally, clear the dictionary through the clear() function:
a = {"name": "xiaoming", "sex": "male", "age": 20, "tel": 134567} a.pop("age") print(a) a.popitem() print(a) a.clear() print(a)
The operation results are as follows:
In addition, you can also output a single element in the dictionary or empty all elements through the del statement.
For example, the following python code:
a = {"name": "xiaoming", "sex": "male", "age": 20, "tel": 134567} del a["tel"] print(a) del a print(a)
The running results are as follows. The reason for the error in the second print function here is that the del statement has deleted dictionary a, so dictionary a is not defined:
3. Dictionary modification
The dictionary elements can be modified by changing the existing elements in the dictionary. It is similar to adding a dictionary. It can also be modified by specifying a key or an update function.
For example, the following python code:
a = {"name": "xiaoming", "sex": "male", "age": 20} a["name"] = "xiaofang" print(a) a.update(sex="female") print(a)
The operation results are as follows:
4. Dictionary query
The key(), values(), and items() functions are used to return the corresponding keys, values, and elements, all of which will return dict respectively_ keys,dict_values,dict_items are the corresponding objects, and these objects can be iterated, so you can traverse the output through the for loop.
Query all the keys in the dictionary by using the keys() function.
For example, the following codes:
a = {"name": "xiaoming", "sex": "male", "age": 20} print(a.keys()) print("adopt for Loop through and output all keys:") for i in a.keys(): print(i)
The operation results are as follows:
Use the values() function to query all the values in the dictionary.
For example, the following python code:
a = {"name": "xiaoming", "sex": "male", "age": 20} print(a.values()) print("adopt for Loop through and output all values:") for i in a.values(): print(i)
The operation results are as follows:
Use the items() function to query all the elements in the dictionary.
For example, the following python code:
a = {"name": "xiaoming", "sex": "male", "age": 20} print(a.items()) print("adopt for Loop through and output all elements:") for i in a.items(): print(i)
The operation results are as follows:
2, Assemble
The set type is unordered, in which the elements cannot be repeated. It is the same as the set in mathematics.
(1) Classification of sets
Sets are divided into variable sets and immutable sets, that is, whether the elements in the set are variable.
(2) Creation of collections
Like the general creation method of a dictionary, a variable set is also created through curly braces, that is, {}, and multiple elements are separated by commas.
Alternatively, you can use the set() function to create a set, or you can directly use set() to create an empty set instead of curly braces, otherwise an empty dictionary will be created.
For example, the following Python code passes a list and tuple into sets y and z:
x = {1, 0, "python", "css", "c++"} print(x) y = set(["t", "h", "e"]) z = set(("t", "h", "e")) print(y, z)
The operation results are as follows:
Immutable sets are created by using the frozenset() function, and the elements in the set cannot be changed.
(3) Addition of collection elements
You can add a set element to a variable set through the add() function and the update() function. The add() function and the update() function add an element and add multiple elements respectively, and the add() function adds the element as a whole to the set, while the update() function splits the element into multiple elements to add.
For example, the following python code:
a = {1, 2, 3, 4} print(a) a.add(5) print(a) b = {"one", "two"} b.update("three", "four") print(b)
The operation results are as follows:
(4) Deletion of collection
You can delete the specified elements in the variable set through the remove() function and the discard() function. The difference is that the discard() function does not perform any operation when deleting elements that do not exist in the set, and the remove() function will report an error.
The pop() function is used to delete random elements from a mutable set.
You can also clear all the elements in the variable set through the clear() function or delete the set directly with the del statement.
For example, the following python code:
a = {1, 2, 3, 4} print(a.clear()) b = {3, 4} del b
The operation results are as follows:
(5) Set type operator
In Python, you can operate on collections through collection type operators.
name | Symbol | meaning |
---|---|---|
union | | | The union of two sets forms a new set |
intersection | & | The intersection of two sets forms a new set |
Difference compensation | - | Excluding the intersection, the elements belonging to only one party will form a new set |
Symmetric difference | ^ | Exclude the intersection of two sets to form a new set |
For example, the following python code:
x = {1, 2, 3, -1} y = {0, 2, 6} print(x | y) print(x & y) print(x - y) print(y - x) print(x ^ y)
The operation results are as follows:
3, Summary of lists, tuples, dictionaries, collections
In Python, combination types are divided into sequence types, set types and mapping types. Sequence types are lists and tuples, which are more orderly than the other two types; The collection type is unordered and the elements are not repeated; The mapping type is represented in the form of key value.
The following is a comparison of lists, tuples, dictionaries and Collections: