With its beautiful syntax and convenient built-in data structures, python has won the affection of many programmers.
One very useful data structure that is very simple to use is the dictionary (dict). When it comes to traversing a dict structure, I think most people think of the for key in dictobj method, and indeed this method works in most cases. But it is not completely safe, see the following example:
# Initialize a dict here
>>> d = {'a':1, 'b':0, 'c':1, 'd':0}
# The intention is to iterate through the dict and delete the elements if they have a value of 0.
>>> for k in d:
... if d[k] == 0:
... del(d[k])
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
RuntimeError: dictionary changed size during iteration
# The result throws an exception, two 0's, and only one element is deleted.
>>> d
{'a': 1, 'c': 1, 'd': 0}
>>> d = {'a':1, 'b':0, 'c':1, 'd':0}
#() is an array of subscripts
>>> ()
['a', 'c', 'b', 'd']
# Traversing like this is fine, because in fact it's actually the list constant () that's being traversed here.
>>> for k in ():
... if d[k] == 0:
... del(d[k])
...
>>> d
{'a': 1, 'c': 1}
# It turned out right, too
>>>
Actually, this example was simplified for me, and I found this problem in a multi-threaded program, so here's my advice: get in the habit of using for k in () when traversing dicts.
However, if it is multi-threaded, is it absolutely safe? Not necessarily: when both threads have finished fetching (), if both threads delete the same key, the one that deletes it first will succeed, and the one that deletes it later will definitely report a KeyError, which can only be guaranteed in some other way.
another article:dict two kinds of traversal performance comparison
On tangling with the performance of dict traversal with and without parentheses
for (d,x) in ():
print "key:"+d+",value:"+str(x)
for d,x in ():
print "key:"+d+",value:"+str(x)
Results of performance tests with and without brackets.
Test results
Number of test strips:15
Start time with brackets:2012-06-14 12:13:37.375000
End time with brackets:2012-06-14 12:13:37.375000
Time interval:0:00:00
Start time without brackets:2012-06-14 12:13:37.375000
End time without brackets:2012-06-14 12:13:37.375000
Time interval:0:00:00
Number of test strips:50
Start time with brackets:2012-06-14 12:13:57.921000
End time with brackets:2012-06-14 12:13:57.921000
Time interval:0:00:00
Start time without brackets:2012-06-14 12:13:57.921000
End time without brackets:2012-06-14 12:13:57.937000
Time interval:0:00:00.016000
Number of test strips:100
Start time with brackets:2012-06-14 11:53:57.453000
End time with brackets:2012-06-14 11:53:57.468000
Time interval:0:00:00.015000
Start time without brackets:2012-06-14 11:53:57.468000
End time without brackets:2012-06-14 11:53:57.531000
Time interval:0:00:00.063000
Number of test strips:150
Start time with brackets:2012-06-14 12:00:54.812000
End time with brackets:2012-06-14 12:00:54.828000
Time interval:0:00:00.016000
Start time without brackets:2012-06-14 12:00:54.828000
End time without brackets:2012-06-14 12:00:54.921000
Time interval:0:00:00.093000
Number of test strips:200
Start time with brackets:2012-06-14 11:59:54.609000
End time with brackets:2012-06-14 11:59:54.687000
Time interval:0:00:00.078000
Start time without brackets:2012-06-14 11:59:54.687000
End time without brackets:2012-06-14 11:59:54.734000
Time interval:0:00:00.047000
Number of test strips:500
Start time with brackets:2012-06-14 11:54:39.906000
End time with brackets:2012-06-14 11:54:40.078000
Time interval:0:00:00.172000
Start time without brackets:2012-06-14 11:54:40.078000
End time without brackets:2012-06-14 11:54:40.125000
Time interval:0:00:00.047000
Number of test strips:1000
Start time with brackets:2012-06-14 11:54:49.171000
End time with brackets:2012-06-14 11:54:49.437000
Time interval:0:00:00.266000
Start time without brackets:2012-06-14 11:54:49.437000
End time without brackets:2012-06-14 11:54:49.609000
Time interval:0:00:00.172000
Number of test strips:2000
Start time with brackets:2012-06-14 11:54:58.921000
End time with brackets:2012-06-14 11:54:59.328000
Time interval:0:00:00.407000
Start time without brackets:2012-06-14 11:54:59.328000
End time without brackets:2012-06-14 11:54:59.687000
Time interval:0:00:00.359000
Number of test strips:5000
Start time with brackets:2012-06-14 11:55:05.781000
End time with brackets:2012-06-14 11:55:06.734000
Time interval:0:00:00.953000
Start time without brackets:2012-06-14 11:55:06.734000
End time without brackets:2012-06-14 11:55:07.609000
Time interval:0:00:00.875000
Number of test strips:10000
Start time with brackets:2012-06-14 11:55:15.656000
End time with brackets:2012-06-14 11:55:17.390000
Time interval:0:00:01.734000
Start time without brackets:2012-06-14 11:55:17.390000
End time without brackets:2012-06-14 11:55:19.109000
Time interval:0:00:01.719000
Number of test strips:20000
Start time with brackets:2012-06-14 12:19:14.921000
End time with brackets:2012-06-14 12:19:18.593000
Time interval:0:00:03.672000
Start time without brackets:2012-06-14 12:19:18.593000
End time without brackets:2012-06-14 12:19:22.218000
Time interval:0:00:03.625000
We can see that when the number of dicts is less than 200, the performance of the bracketed one is higher, but after 200 or more data, the execution time of the bracketed one is less.
Here is the test code.
Test Code
#-*- coding: utf-8 -*-
import datetime,codecs
dict = {}
for i in xrange(0,20000):
("name"+str(i))
dict["name"+str(i)]="name"
s=(r'c:\\','a', 'utf-8')
def write(des):
(("utf-8"))
write("Number of test bars:")
write(str(len(dict))+"\r\n")
write("Bracketed start time:")
a=()
(str(a)+"\r\n")
for (d,x) in ():
print "key:"+d+",value:"+str(x)
write("End time with brackets:")
b=()
write(str(b)+"\r\n")
write("Time interval:")
write(str(b-a)+"\r\n")
write("Start time without brackets:")
c=()
write(str(c)+"\r\n")
for d,x in ():
print "key:"+d+",value:"+str(x)
write("End time without brackets:")
d=()
write(str(d)+"\r\n")
write("Time interval:")
write(str(d-c)+"\r\n")
write("\r\n")
()
Is there a good solution to the Chinese garbled code problem ....?