python /我可以将定义的字典传递给** kwargs吗? [英] python/can i pass a defined dictionary to **kwargs?

查看:511
本文介绍了python /我可以将定义的字典传递给** kwargs吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次在这里发帖。希望我能得到很好的建议:)我已经学会了如何将** kwargs和* args都传递给一个函数,并且效果很好。类似于以下内容:

this is my first time posting here. hopefully i can get nice advice:) i have learnt how to pass both **kwargs and *args into a function, and it worked pretty well. like the following:

def market_prices(name, **kwargs):
     print("Hello! Welcome to "+name+" Market!")
     for fruit, price in kwargs.items():
         price_list = " {} is NTD {} per piece.".format(fruit,price)
         print (price_list) 
market_prices('Wellcome',banana=8, apple=10)

但是,在实际情况下,我宁愿预定义具有大量键值的字典,因此在调用函数时不必键入每个参数。我在网上搜索过,但找不到很好的例子或解释:/您能给我一些建议吗?这是我尝试说的代码:

however in real case, i'd rather pre-defined a dictionary with lots of key&value, so don't have to type in every parameter when calling my function. i have searched online but cannot find a good example or explanation:/ could you give me some advice? here is the code i try to say:

fruits:{"apple":10,
       "banana":8,
       "pineapple":50,
       "mango":45
       }

def market_prices(name, **fruits):
    print("Hello! Welcome to "+name+" Market!")
    for fruit, price in fruits.items():
        price_list = " {} is NTD {} per piece.".format(fruit,price)
        print (price_list)

market_prices('Wellcome ', fruits)

NameError:未定义名称水果

NameError: name 'fruits' is not defined

非常感谢!:)

推荐答案

可能有4种情况:

您使用命名参数调用函数,并且您希望<函数中的strong>命名变量:

(请注意默认值)

You call the function using named arguments and you want named variables in the function:
(note the default values)

def buy(orange=2, apple=3):
    print('orange: ', orange)
    print('apple: ', apple)

buy(apple=4)
# orange:  2
# apple:  4

您可以使用命名参数调用函数,但您希望<
,然后在函数定义中使用 ** dictionaryname 来收集传递的参数

You call the function using named arguments but you want a dictionary in the function:
then use **dictionaryname in the function definition to collect the passed arguments

def buy(**shoppinglist):
    for name, qty in shoppinglist.items():
        print('{}: {}'.format(name, qty) )

buy(apple=4, banana=5)
# banana: 5
# apple: 4

您调用传递字典的函数,但是您想要命名变量中的函数:

在调用该函数以解压缩字典时使用 ** dictionaryname

You call the function passing a dictionary but you want named variables in the function:
use **dictionaryname when calling the function to unpack the dictionary

def buy(icecream=1, apple=3, egg=1):
    print('icecream:', icecream)
    print('apple:', apple)
    print('egg:', egg)

shoppinglist = {'icecream':5, 'apple':1}
buy(**shoppinglist)
# icecream: 5
# apple: 1
# egg: 1

您调用传递字典的函数,并且您要在函数中词典

只需通过词典

You call the function passing a dictionary and you want a dictionary in the function:
just pass the dictionary

def buy(shoppinglist):
    for name, qty in shoppinglist.items():
        print('{}: {}'.format(name, qty) )

shoppinglist = {'egg':45, 'apple':1}
buy(shoppinglist)
# egg: 45
# apple: 1

这篇关于python /我可以将定义的字典传递给** kwargs吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆