我希望以下列方式显示输出keys: values:
dad:bob
Mom:lisa
brother : joe
& so on
Run Code Online (Sandbox Code Playgroud)
但只有值显示在输出中.
我应该在此代码中进行哪些更改才能获得所需的输出?
d = dict(Dad='Bob', Mom='Lisa', Brother= 'joe')
def f2(Dad,Mom,Brother):
print Dad,Mom,Brother
f2(**d)
Run Code Online (Sandbox Code Playgroud)
使用**kwargs处理函数的关键字参数:
d = dict(Dad='Bob', Mom='Lisa', Brother= 'joe')
def f2(**kwargs):
for key, value in kwargs.iteritems():
print '%s:%s' % (key, value)
f2(**d)
Run Code Online (Sandbox Code Playgroud)
打印:
Dad:Bob
Brother:joe
Mom:Lisa
Run Code Online (Sandbox Code Playgroud)
另见: