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()