本文介绍了从一个函数调用字典到另一个函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将在一个函数中创建的字典称为另一个函数?
How can I call a dictionary created in one function to another?
我尝试使用但它对我不起作用.
I have tried using How do I access a dictionary from a function to be used in another function? but it doesn't work for me.
我已经在server()
中创建了dictionary1
,并且我想在create_csv()
中使用它.
I have created dictionary1
in server()
and I want to use it in create_csv()
.
我怎么称呼它?
def server(id):
dictionary1 = dict(zip(temp_sourcenodes, sip))
dictionary1.update(dict(zip(temp_destnodes, dip)))
print(dictionary1)
def create_csv():
推荐答案
使用return
并从create_csv
内部调用server
.这可能需要将id_
馈送到create_csv
,但这可能是合理的,因为大概dictionary1
是基于id_
构造的.
Use return
and call server
from within create_csv
. This may necessitate feeding id_
to create_csv
, but this is likely reasonable, as presumably dictionary1
is constructed based on id_
.
def server(id_):
# some code to construct dictionary1
return dictionary1
def create_csv(id_):
my_dict = server(id_)
# export to csv here
这篇关于从一个函数调用字典到另一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!