Comparison of methods of Python two traversal dictionaries (dict) _python

Source: Internet
Author: User
Tags data structures datetime time interval

Python, with its graceful syntax and handy built-in data structure, has won the close gaze of many programmers.
One of the most useful data structures is the dictionary (dict), which is very simple to use. When it comes to traversing a dict structure, I think most people will think about the method for the key in Dictobj, which is true for most cases. But not completely safe, see the following example:

Copy Code code as follows:

#这里初始化一个dict
>>> d = {' A ': 1, ' B ': 0, ' C ': 1, ' d ': 0}
#本意是遍历dict, the value of the element is found to be 0, delete
>>> 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
#结果抛出异常了, two 0 of the elements, also only delete one.
>>> D
{' A ': 1, ' C ': 1, ' d ': 0}

>>> d = {' A ': 1, ' B ': 0, ' C ': 1, ' d ': 0}
#d. Keys () is an array of subscripts
>>> D.keys ()
[' A ', ' C ', ' B ', ' d ']
#这样遍历, there is no problem, because in fact, here is traversing the D.keys () this list constant.
>>> for K in D.keys ():
... if d[k] = = 0:
... del (d[k])
...
>>> D
{' A ': 1, ' C ': 1}
#结果也是对的
>>>

In fact, this example is my simplification, I was in a multithreaded program to find this problem, so my advice is: Traverse Dict, the habit of using for K-in D.keys ().
However, if it is multi-threaded, this is absolutely safe? Not necessarily: When two threads are finished D.keys (), if two threads are to delete the same key, the first deletion will be successful, after the deletion of the affirmation will be reported Keyerror, this seems only through other means to ensure that.


Another article: Dict performance comparison of two kinds of traversal modes

On the performance problems with parentheses and without parentheses in entangled dict traversal

Copy Code code as follows:

for (d,x) in Dict.items ():
print ' key: ' +d+ ', Value: ' +str (x)

For d,x in Dict.items ():
print ' key: ' +d+ ', Value: ' +str (x)

Performance test results with parentheses and without parentheses:

Copy Code code as follows:

Test results
Number of test lines: 15
Parenthesized start time: 2012-06-14 12:13:37.375000
Parenthesized End time: 2012-06-14 12:13:37.375000
Time interval: 0:00:00
Without parenthesis start time: 2012-06-14 12:13:37.375000
No parenthesis end time: 2012-06-14 12:13:37.375000
Time interval: 0:00:00

Number of test lines: 50
Parenthesized start time: 2012-06-14 12:13:57.921000
Parenthesized End time: 2012-06-14 12:13:57.921000
Time interval: 0:00:00
Without parenthesis start time: 2012-06-14 12:13:57.921000
No parenthesis end time: 2012-06-14 12:13:57.937000
Time interval: 0:00:00.016000
Number of test lines: 100
Parenthesized start time: 2012-06-14 11:53:57.453000
Parenthesized End time: 2012-06-14 11:53:57.468000
Time interval: 0:00:00.015000
Without parenthesis start time: 2012-06-14 11:53:57.468000
No parenthesis end time: 2012-06-14 11:53:57.531000
Time interval: 0:00:00.063000

Number of test lines: 150
Parenthesized start time: 2012-06-14 12:00:54.812000
Parenthesized End time: 2012-06-14 12:00:54.828000
Time interval: 0:00:00.016000
Without parenthesis start time: 2012-06-14 12:00:54.828000
No parenthesis end time: 2012-06-14 12:00:54.921000
Time interval: 0:00:00.093000

Number of test lines: 200
Parenthesized start time: 2012-06-14 11:59:54.609000
Parenthesized End time: 2012-06-14 11:59:54.687000
Time interval: 0:00:00.078000
Without parenthesis start time: 2012-06-14 11:59:54.687000
No parenthesis end time: 2012-06-14 11:59:54.734000
Time interval: 0:00:00.047000

Number of test lines: 500
Parenthesized start time: 2012-06-14 11:54:39.906000
Parenthesized End time: 2012-06-14 11:54:40.078000
Time interval: 0:00:00.172000
Without parenthesis start time: 2012-06-14 11:54:40.078000
No parenthesis end time: 2012-06-14 11:54:40.125000
Time interval: 0:00:00.047000

Number of test lines: 1000
Parenthesized start time: 2012-06-14 11:54:49.171000
Parenthesized End time: 2012-06-14 11:54:49.437000
Time interval: 0:00:00.266000
Without parenthesis start time: 2012-06-14 11:54:49.437000
No parenthesis end time: 2012-06-14 11:54:49.609000
Time interval: 0:00:00.172000

Number of test lines: 2000
Parenthesized start time: 2012-06-14 11:54:58.921000
Parenthesized End time: 2012-06-14 11:54:59.328000
Time interval: 0:00:00.407000
Without parenthesis start time: 2012-06-14 11:54:59.328000
No parenthesis end time: 2012-06-14 11:54:59.687000
Time interval: 0:00:00.359000

Number of test lines: 5000
Parenthesized start time: 2012-06-14 11:55:05.781000
Parenthesized End time: 2012-06-14 11:55:06.734000
Time interval: 0:00:00.953000
Without parenthesis start time: 2012-06-14 11:55:06.734000
No parenthesis end time: 2012-06-14 11:55:07.609000
Time interval: 0:00:00.875000

Number of test lines: 10000
Parenthesized start time: 2012-06-14 11:55:15.656000
Parenthesized End time: 2012-06-14 11:55:17.390000
Time interval: 0:00:01.734000
Without parenthesis start time: 2012-06-14 11:55:17.390000
No parenthesis end time: 2012-06-14 11:55:19.109000
Time interval: 0:00:01.719000

Number of test lines: 20000
Parenthesized start time: 2012-06-14 12:19:14.921000
Parenthesized End time: 2012-06-14 12:19:18.593000
Time interval: 0:00:03.672000
Without parenthesis start time: 2012-06-14 12:19:18.593000
No parenthesis end time: 2012-06-14 12:19:22.218000
Time interval: 0:00:03.625000


As we can see, the Dict bar count is a bit higher with parentheses at 2001, but less execution time after 200 or more data without parentheses.

Here is the test code:

Copy Code code as follows:

Test code
#-*-Coding:utf-8-*-
Import Datetime,codecs

Dict = {}

For I in Xrange (0,20000):
Dict.setdefault ("name" +str (i))
dict["Name" +str (i)]= "name"

S=codecs.open (R ' C:\\dict.txt ', ' a ', ' utf-8 ')

def write (des):
S.write (Des.decode ("Utf-8"))

Write ("Number of test strips:")
Write (str (dict) + "\ r \ n")
Write ("Parenthesized start Time:")
A=datetime.datetime.now ()
S.write (str (a) + "\ r \ n")

for (d,x) in Dict.items ():
print ' key: ' +d+ ', Value: ' +str (x)
Write ("Parenthesized End Time:")
B=datetime.datetime.now ()
Write (str (b) + "\ r \ n")
Write ("time interval:")
Write (str (b-a) + "\ r \ n")

Write ("No parenthesis Start time:")
C=datetime.datetime.now ()
Write (str (c) + "\ r \ n")
For d,x in Dict.items ():
print ' key: ' +d+ ', Value: ' +str (x)
Write ("No Parenthesis End time:")
D=datetime.datetime.now ()
Write (str (d) + "\ r \ n")
Write ("time interval:")
Write (str (d-c) + "\ r \ n")
Write ("\ r \ n")
S.close ()

Chinese garbled problem there is no good solution ...?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.