ValueError:在Python Dictionary中解压缩的值太多

时间:2013-07-24 10:01:03

标签: python dictionary

我有一个接受字符串,列表和字典的函数

def superDynaParams(myname, *likes, **relatives): # *n is a list and **n is dictionary
    print '--------------------------'
    print 'my name is ' + myname

    print 'I like the following'

    for like in likes:
        print like

    print 'and my family are'

    for key, role in relatives:
        if parents[role] != None:
             print key + ' ' + role

但它返回错误

  

ValueError:要解压缩的值太多

我的参数是

superDynaParams('Mark Paul',
                'programming','arts','japanese','literature','music',
                father='papa',mother='mama',sister='neechan',brother='niichan')

2 个答案:

答案 0 :(得分:14)

你正在循环字典:

for key, role in relatives:

但只产生,所以一次只能产生一个对象。如果要循环键和值,请使用dict.items()方法:

for key, role in relatives.items():

在Python 2上,使用dict.iteritems()方法提高效率:

for key, role in relatives.iteritems():

答案 1 :(得分:0)

您应该使用迭代器来迭代项目:

relatives.iteritems()

for relative in relatives.iteritems():
    //do something