On 22/05/07, John Washakie <washakie at gmail.com> wrote:
> I have a Dictionary, that is made up of keys which are email
> addresses, and values which are a list of firstname, lastnamet,
> address, etc...
>
> If I run the following:
>
> last = {}
[...]
> for k,v in last:
> print "Email: %s , has the Last name: %s" % (k,v[0])
>
> I get the error indicated in the subject:
> ValueError: too many values to unpack
The "implicit" iteration that dictionaries support only iterates over keys.
i.e. you could have done this:
for k in last:
print "Key is %s, value is %s" % (k, last[k])
Alternatively, you can use the iteritems() method;
for k, v in last.iteritems():
print "Key is %s, value is %s" % (k, v)
简单翻译一下,python只支持对于key的遍历,所以不能使用for k,v这种形式,
这个时候会提示ValueError: too many values to unpack。
我们在遍历字典的时候可以用for k,v in last.iteritems()