Kif*_*tau 2 python dictionary return
我的问题是我有两个函数,其中一个返回值是以下列方式的两个字典:
def fnct1():
return dict1, dict2
Run Code Online (Sandbox Code Playgroud)
所以它返回到我的另一个函数,返回值是前一个函数的两个字典,也是一个新的字典,所以像这样的东西
def fnct2():
return dict3, fnct(1)
Run Code Online (Sandbox Code Playgroud)
这个问题是有以下结果:
({dict3},({dict1},{dict2})
Run Code Online (Sandbox Code Playgroud)
但我希望它看起来如下:
({dict3},{dict1},{dict2})
Run Code Online (Sandbox Code Playgroud)
您可以fnct1()在返回之前解压缩值:
def fnct2():
dict1, dict2 = fnct1()
return dict3, dict1, dict2
Run Code Online (Sandbox Code Playgroud)
由于您的函数返回一个元组,您需要返回单个元组项,或解包它们:
def fnct1():
dict1 = { "name": "d1" }
dict2 = { "name": "d2" }
return dict1, dict2
def fnct2():
dict3 = { "name": "d3" }
res = fnct1()
return dict3, res[0], res[1] # return the individual tuple elements
# alternative implementation of fnct2:
def fnct2():
dict3 = { "name": "d3" }
d1, d2 = fnct1() # unpack your tuple
return dict3, d1, d2
print(fnct2())
# Output: ({'name': 'd3'}, {'name': 'd1'}, {'name': 'd2'})
Run Code Online (Sandbox Code Playgroud)