python错误for k,v in d: ValueError: too many values to unpack

John Fouhy john at fouhy.net
Tue May 22 00:50:14 CEST 2007


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,使用粗体的形式代替。
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值