#!/usr/bin/python
#coding: utf-8
d = {' Adam ': $, ' Lisa ': ", ' Bart ':", "Paul": "The", "
the key of the iteration dict" for
x i n D.keys ():
print x
# can also be used in this way to iterate
for x in D.iterkeys ():
The value of the dict of the print X # Iteration for
x in D.values (): C10/>print x
for X in D.itervalues ():
print x
# Iteration key value pair for
K, v. in D.items ():
print K, ":", v
for K, V in D.iteritems ():
print K, ":", v
' "The
above code, there are two ways to iterate between a key value or a key value, in the case of values
and Itervalues,
explain the difference between
the two ways 1. The values () method actually converts a dict into a list containing value.
2. However, the Itervalues () method does not convert, and it takes value out of the dict in turn,
so the Itervalues () method saves the memory needed to generate the list, rather than the values () method.
3. Print itervalues () finds that it returns a <dictionary-valueiterator> object, which shows that in Python the for
Loop can act as an iterative object far more than List,tuple, Str,unicode,dict and so on,
any iteration object can be used for a for loop, and internal how to iterate we don't usually care about
print d.itervalues ()
# < Dictionary-valueiterator object at 0x0137f8a0>
about iterations
If an object says that it can iterate, then we iterate over it directly with a for loop, so that the iteration is an abstract data operation,
It does not have any requirements for the data inside the iteration object.
'''