Python dict dictionary and some examples of value assignment reference (detailed description), pythondict
Recently, I am working on a very large database that needs to be searched by numerical value. So I thought of the dictionary in python, and I have never used dict.
List and tuple are the most commonly used)
I have read the following method to create a dictionary:
Method 1:
Dict = {'name': 'Earth ', 'Port': 80}
Method 2:
Fdict = dict (['x', 1], ['y', 2])
Method 3:
Ddict = {}. fromkeys ('x', 'y'),-1)
I tried these methods and found that they were not easy to use. I couldn't make the desired results, because the database found the results of the tuple type and could not be changed. In method 2, make sure that
Yes list (here, the tuple and list type interchange Methods l = tuple (l) l = list (l ))
When I did the exercises, I suddenly saw a method to declare it first.
Fdict = {}
Then
Fdict [keys] = values
Then, we can continue to loop through this formula. The results can be added to fdict one by one without being overwritten. Previously, we thought it would be overwritten by the new one by default.
In this way, the result is converted into a key-value pair.
In python, the value is actually the address:
Example:
A = [1, 2, 4]
B =
A. append (1)
Print
Print B
The two results are: [1, 2, 3, 4, 1].
This indicates that when a variable in python assigns a value to another variable, it transmits an address. Therefore, when a points to a, B obtains a pointer to, so the results will also
The output result is the same as that of.
More experiments:
A = [1, 2, 4]
B =
A + = [1] # Add a list value at the end
Print
Print B
The result is as follows:
[1, 2, 3, 4, 1]
[1, 2, 3, 4, 1]
There is no problem with this. It is the same as the above explanation that the address is passed. No matter how a is added, B is the same output as a address.
Next experiment:
A = [1, 2, 4]
B =
A = a + [1] # Add a list value at the end
Print
Print B
You can try to see the result.
The running result is:
[1, 2, 3, 4, 1]
[1, 2, 3, 4]
Why?
Why is the result different after adding + = and separation?
By checking the information, I am confident in the following explanation:
When a = a + [1], what the system does is to put the result of a + [1] In another address c, and then point a to this address c, so when I Output a, the result is conceivable.
However, B still points to the previous position of a, and the value of the previous position has not changed, so B will output such a slightly surprising value.
The "+ =" operation is still performed on the address pointed to by a, so B will also change.
Summary:When a variable in python assigns a value to another variable (=), it is not a value, but a pointer address. Be careful when performing this operation to avoid pitfalls.
The above python dict dictionary and some examples of value assignment reference (detailed description) are all the content shared by the editor. I hope to give you a reference and support for the help house.