In this paper, the use of the Dictionary items () series functions in Python is described, which has a good reference value for Python program design. The specific analysis is as follows:
Let's look at an example first:
Import HTML # available only in Python 3.x def make_elements (name, Value, **attrs): keyvals = ['%s= '%s '% item F or item in Attrs.items ()] attr_str = '. Join (keyvals) element = ' <{name}{attrs}>{value}
'. Format ( name = name, attrs = attr_str, value = Html.escape (value)) return element make_elements (' item ' , ' Albatross ', size= ' large ', quantity=6) make_elements (' P ', '
)
The function of the program is very simple, is to generate HTML tags, note that the HTML module can only be in Python 3.x.
At first I just noticed that the keyvals that generated the tag attribute list is very interesting in the way that the dictionary type variable is built, and two%s corresponds to an item, so I looked up the relevant data and pulled out a lot of things and summarized it here.
Note: The following versions of all Python interpreters are used, and 2.x corresponds to the 2.7.3,3.x corresponding to the 3.4.1
In Python 2.x, the method of items in the official document is this: Generate a list of pairs (key, value) as follows:
In the process of searching, I inadvertently see the question on StackOverflow: What is the difference between dict.items () and Dict.iteritems ()? , the first answer roughly means this:
"At first items () was to return a list like the one above that contained all the elements of the dict, but because of this too much memory, it was added later (note: Starting in Python 2.2) Iteritems (), Iterkeys (), itervalues () This set of functions is used to return a iterator to save memory, but in 3.x the items () itself returns such iterator, so the behavior of items () in 3.x is consistent with the 2.x iteritems () behavior, Iteritems () this A set of functions is abolished. ”
But more interestingly, although the answer is accepted, the following comments point out that this is not accurate, in 3.x the behavior of items () and 2.x iteritems () is not the same, it actually returns a "full Sequence-protocol object, which reflects the change of the Dict, and then joins another function in Python 2.7, Viewitems () and 3.x, which is consistent with this behavior.
To confirm the comment, I did the following test, observing the Python version that was used in the test:
Test 1 (Python 2.7.3):
Python 2.7.3 (default, Feb, 19:58:35) [GCC 4.6.3] on linux2 Type ' help ', ' copyright ', ' credits ' or ' license ' F or more information. >>> d = {' size ': ' Large ', ' Quantity ': 6} >>> il = D.items () >>> it = D.iteritems () >>> VI = d.viewitems () >>> il [(' Quantity ', 6), (' Size ', ' large ')] >>> it
>>> VI dict _items ([' Quantity ', 6), (' Size ', ' large ')])
Test 2 (Python 3.4.1):
Python 3.4.1 (default, 16:43:01) [GCC 4.9.0] on the Linux Type "Help", "copyright", "credits" or "license" fo R more information. >>> d = {' size ': ' Large ', ' Quantity ': 6} >>> il = D.items () >>> it = D.iteritems () Traceback (M OST recent call last): File "
", line 1, in
attributeerror: ' Dict ' object have no attribute ' Iteritems ' >>> VI = d.viewitems () Traceback (most recent call last): File "
", line 1, in
attributeerror: ' Dict ' object has no attribute ' viewitems ' >>> il dict_items ([' Size ', ' large '), (' Q Uantity ', 6)])
As you can see in Python 3.x, both Iteritems () and Viewitems () have been abolished, and item () has the same result as the 2.x viewitems ().
The content returned by Iteritems () and Viewitems () in 2.x can be traversed with a for, as follows
>>> for K, V in it: ... Print K, v ... Quantity 6 size Large >>> for K, V in VI: ... Print K, v ...
What is the difference between the two? Viewitems () Returns the View object, which can reflect changes in the dictionary, such as the above example, if the use of it and VI before the two variables to add a key-value combination, the difference is easy to see.
>>> it = d.iteritems () >>> VI = d.viewitems () >>> d[' newkey '] = ' newvalue ' >>> d {' New Key ': ' NewValue ', ' Quantity ': 6, ' size ': ' Large '} >>> vi dict_items ([' Newkey ', ' newvalue '), (' Quantity ', 6), (' s Ize ', ' large ')]) >>> it
>>> for K, V in VI: ... Print K, v ... Newkey NewValue Quantity 6 size Large >>> for K, V in it: ... Print K, v ... Traceback (most recent): File "
", line 1, in
runtimeerror:dictionary Changed size during iteration
In the third line, we insert a new element like D, VI can continue to traverse, and the new traversal can reflect the change of D, but when traversing it, the error indicates that the size of the dictionary during traversal, the traversal failed.
To sum up, in 2.x, initially is the items () this method, but because of too wasted memory, so added the Iteritems () method, used to return a iterator, in 3.x to change the behavior of the items () to return a View object, so that it returned Back of the object can also reflect the original dictionary changes, but also in 2.7 added Viewitems () backward-compatible with this feature.
So in 3.x there is no need to dwell on the differences of the three, because only one of the items () method is retained.
It is believed that the example described in this paper has some reference value for Python program design.