gpt4 book ai didi

python - 将字典传递给以元组作为键的函数时生成的关键字错误

转载 作者:行者123 更新时间:2023-12-05 06:55:21 24 4
gpt4 key购买 nike

我是 Python 的新手,我正在努力完成将字典(其键是元组)作为函数参数传递的任务。

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

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

tupletest(**mydict)

生成以下关键字错误:

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

鉴于错误信息,我不确定这是否可行。我正在 3.7.4

中对此进行测试

感谢所有帮助。

最佳答案

我做了一个小例子。这是可能的:

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)

希望对您有所帮助。我还实现了一个输入字典键的小例子。

Output

关于python - 将字典传递给以元组作为键的函数时生成的关键字错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65392503/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com