现在的位置: 首页 > 综合 > 正文

python中字典的可变参数

2019年08月04日 ⁄ 综合 ⁄ 共 411字 ⁄ 字号 评论关闭

在python中可以用def XXX(**args)的定义形式来定义可变参数的函数

 

同时args将被视为字典dict,使用如下:

该示例将dict中的item存储到列表中

def diccat(**args):
       total=[]   
       for key,item in args.items():
              total += item
       return total
dicttest={'1':'a','2':'b','3':'c','4':'5'}
print (diccat(**dicttest))

结果如下:

['a', 'c', 'b', '5']

去掉**也可以:

def diccat(args):
       total=[]   
       for key,item in args.items():
              total += item
       return total
dicttest={'1':'a','2':'b','3':'c','4':'5'}
print (diccat(dicttest))

结果:
['a', 'c', 'b', '5']

抱歉!评论已关闭.