简体   繁体   English

将字典传递给以元组为键的 function 时生成关键字错误

[英]keyword error generated when Passing a dictionary to a function with tuples as the keys

I am new to Python and I am struggling with the task of passing a dictionary, whose keys are tuples, as an argument to a function.我是 Python 的新手,我正在努力完成传递字典的任务,字典的键是元组,作为 function 的参数。

mydict = {('hostabc', 'pola'): 333444567, ('hostdef', 'polb'): 111222333, ('hostghi', 'polc'): 222999888}

def tupletest(**kwargs):
    print(kwargs)

tupletest(**mydict)

The following keyword error is generated:生成以下关键字错误:

TypeError                                 Traceback (most recent call last)
<ipython-input-29-fec409a1eb53> in <module>
      2 def tupletest(**kwargs):
      3     print(kwargs)
----> 4 tupletest(**mydict)

TypeError: tupletest() keywords must be strings

I am unsure if this is even possible given the error msg.鉴于错误消息,我不确定这是否可能。 I am testing this in 3.7.4我在3.7.4中对此进行测试

All help appreciated.所有帮助表示赞赏。

I performed a little example.我做了一个小例子。 It ist possible:有可能:

mydict = {('hostabc', 'pola'): 333444567, ('hostdef', 'polb'): 111222333, ('hostghi', 'polc'): 222999888}

def tupletest(kwargs):
    for key in kwargs:
        #print("key: %s , value: %s" % (key, kwargs[key]))
        print(key[0])
        print(key[1])

tupletest(mydict)

I hope this helps you.我希望这可以帮助你。 I also implemented a little example for entering the key of the dictionary.我还实现了一个输入字典键的小例子。

Output Output

Short answer is, no it's not possible.简短的回答是,不,这是不可能的。

complex(real=3, imag=5)
complex(**{'real': 3, 'imag': 5})

**kwargs represents keyword arguments. **kwargs代表关键字 arguments。 These arguments are unpacked (so to say) and passed to the function.这些 arguments 被解包(可以这么说)并传递给 function。 That way you can use them in the function without having to explicitly pass them to the function as the positional-or-keyword arguments.这样,您可以在 function 中使用它们,而无需将它们作为位置或关键字 arguments 显式传递给 function。

def func(*args, **kwargs): ...

var-keyword: specifies that arbitrarily many keyword arguments can be provided (in addition to any keyword arguments already accepted by other parameters). var-keyword:指定可以提供任意多个关键字arguments(除了已经被其他参数接受的任何关键字arguments)。 Such a parameter can be defined by prepending the parameter name with **, for example kwargs in the example above.可以通过在参数名称前加上 ** 来定义这样的参数,例如上面示例中的 kwargs。

https://docs.python.org/3/glossary.html#term-argument https://docs.python.org/3/glossary.html#term-argument

@BoarGules has nicely pointed you to the path. @BoarGules 很好地为您指明了路径。 I have nothing new to add and I am saying below the same thing but in a little verbose manner.我没有什么新东西要补充的,我在下面说的是同一件事,但有点冗长。

See this nice discussion here .在这里看到这个很好的讨论。 So dictionary keys become the named parameters to the function.因此字典键成为 function 的命名参数。 In this particular case however the keys are tuples.然而,在这种特殊情况下,键是元组。 The keyword must have an associated string property and that is what the error is saying above.关键字必须具有关联的字符串属性,这就是上面的错误所说的。 Notice the " strings " in the error message below.请注意下面错误消息中的“字符串”。

TypeError: tupletest() keywords must be strings

Had your dictionary been simpler like below, it would have worked.如果你的字典像下面这样更简单,它会起作用的。

mydict = {"a": 333444567, "b": 111222333, "c": 222999888}


def tupletest(**kwargs):
    for k in kwargs:
        print(k)

tupletest(**mydict)

The above gives this.上面给出了这个。

a
b
c

If you would rather want to have the tuples, I will take the dangerous route of eval after quoting the tuples.如果你想拥有元组,我会在引用元组后采取危险的 eval 路线。

mydict = {"('hostabc', 'pola')": 333444567, "('hostdef', 'polb')": 111222333, "('hostghi', 'polc')": 222999888}

def tupletest(**kwargs):
    for k in kwargs:
        print(eval(k))

tupletest(**mydict)

This gives the following output.这给出了以下 output。

('hostabc', 'pola')
('hostdef', 'polb')
('hostghi', 'polc')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM