本文介绍了ValueError:在Python Dictionary中解压缩太多值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个接受字符串,列表和字典的函数。 def superDynaParams(myname,* likes,**亲戚):#* n是一个列表,** n是字典
print'--------------------------'
打印'我的名字是'+我的名字
打印'我喜欢以下'
喜欢喜欢:
打印像
打印'和我的家人是'
为关键,角色在亲戚:
如果父母[角色]!=无:
打印键+''+角色
但它返回错误
我的参数是
superDynaParams('Mark Paul',
'programming','arts','japanese','literature','music',
father ='papa ',mother ='mama',sister ='neechan',brother ='niichan')
解决方案
你正在循环一个字典:
为关键,角色在亲戚:
但是只能产生键,所以一次一个对象。如果要循环键和值,请使用 dict.items()
方法:
for key,role in relatives.items():
在Python 2上,使用 dict.iteritems()
方法实现效率:
,在relatives.iteritems()中的角色:
I have a function that accepts a string, list and a 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
but it returns an error
my parameters are
superDynaParams('Mark Paul',
'programming','arts','japanese','literature','music',
father='papa',mother='mama',sister='neechan',brother='niichan')
解决方案
You are looping over a dictionary:
for key, role in relatives:
but that only yields keys, so one single object at a time. If you want to loop over keys and values, use the dict.items()
method:
for key, role in relatives.items():
On Python 2, use the dict.iteritems()
method for efficiency:
for key, role in relatives.iteritems():
这篇关于ValueError:在Python Dictionary中解压缩太多值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!