John Fouhy john at fouhy.net
Tue May 22 00:50:14 CEST 2007
- Previous message: [Tutor] for k,v in d: ValueError: too many values to unpack
- Next message: [Tutor] MS SQL Connection
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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) See http://www.python.org/doc/current/lib/typesmapping.html for details. -- John. 简单翻译一下,python只支持对于key的遍历,所以不能使用for k,v这种形式,这个时候会提示ValueError: too many values to unpack,使用粗体的形式代替。