Python字典解包

2024-05-19 17:08:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下代码:

def fun(a, b, c): 
    print(a, b, c) 

d = {'a':2, 'b':4, 'c':10} 
fun(**d) 

此代码打印2, 4, 10。我想知道为什么只打印字典值而不打印键


Tags: 代码字典defprintfun
2条回答

使用*表示键,使用**表示值,如下所示

enter image description here

要在函数或方法参数中传递字典,请使用**kwargs

def fun(**kwargs):
    print kwargs     #<  this prints the dictionary with it's keys

d = {'a':2, 'b':4, 'c':10} 
fun(**d)

相关问题 更多 >