python /我可以将定义的字典传递给** kwargs吗?

时间:2018-08-08 16:56:23

标签: python function dictionary kwargs

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

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)

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

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:名称“ fruits”未定义

非常感谢您!:)

4 个答案:

答案 0 :(得分:2)

有4种可能的情况:

您使用命名参数调用该函数,并希望在该函数中使用命名变量
(请注意默认值)

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

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

您使用命名参数调用该函数,但是您希望在该函数中使用字典
然后在函数定义中使用**dictionaryname来收集传递的参数

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

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

您通过字典调用该函数,但是要在该函数中使用命名变量
在调用函数来解压缩字典时使用**dictionaryname

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

您调用传递字典的函数,并希望在函数中字典
只需通过字典

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

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

答案 1 :(得分:1)

您有一个定义fruits的错字。应该是这样的

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

答案 2 :(得分:1)

在水果参数前使用**

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) #Use **before arguments

答案 3 :(得分:0)

感谢你们的快速而有用的评论!在这里,我尝试一些,它的工作原理! 在定义函数时,如果将**用作参数,则请确保在调用它时也将其放入!否则,都不要放! 1.带有**

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)

2。无** 水果= {“苹果”:10,        “香蕉”:8,        “菠萝”:50,        “芒果”: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)

问题已解决:))xoxo