This article illustrates the common method of Python merging two dictionaries with efficiency comparisons. Share to everyone for your reference. The specific analysis is as follows:
The following code illustrates 5 methods of merging two dictionaries and makes a simple performance test
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 The |
|
In addition to the F4 method that can cause destructive modifications to the dictionary D1, several other methods return the merged results as a new dictionary.
Here is the test result:
?
1 2 3 4 5 6 7 |
F1 cost:2.382999897 F2 cost:4.45399999619 f3 cost:3.02100014687 f4 cost:1.73000001907 f5 cost:2.3710000515 f6 cost:2.8970 0007439 F7 cost:2.35600018501 |
It can be seen that F4 is the most efficient, and the F4 method is recommended if you do not need to keep the original dictionary.
I hope this article will help you with your Python programming.