Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用将列表作为输入返回给另一个函数的函数?_Python_Python 3.x_List_Function - Fatal编程技术网

Python 如何使用将列表作为输入返回给另一个函数的函数?

Python 如何使用将列表作为输入返回给另一个函数的函数?,python,python-3.x,list,function,Python,Python 3.x,List,Function,我首先编写了一个函数,它包含18个参数,并将它们转换成6个不同的列表。代码如下: def list_maker(val1,val2,val3,val4,val5,val6,val7,val8,val9,por1,por2,por3,hth1,hth2,hth3,sat1,sat2,sat3): #Make the voip list list1 = [val1,val2,val3] list2 = [val4,val5,val6] list3 = [val7,val8,val9] #Make

我首先编写了一个函数,它包含18个参数,并将它们转换成6个不同的列表。代码如下:

def list_maker(val1,val2,val3,val4,val5,val6,val7,val8,val9,por1,por2,por3,hth1,hth2,hth3,sat1,sat2,sat3):

#Make the voip list
list1 = [val1,val2,val3]
list2 = [val4,val5,val6]
list3 = [val7,val8,val9]

#Make the variable list
list_por = [por1,por2,por3]
list_hth = [hth1,hth2,hth3]
list_sat = [sat1,sat2,sat3]

return list1,list2,list3,list_por,list_hth,list_sat
那部分工作得很好(一旦它真的工作了,我会让它看起来更好)。 现在,我的想法是使用该函数作为下面另一个函数的输入来创建绘图:

def graph_maker(listx1,listx2,listx3,list1,list2,list3):

#plot the saturation graph
por_plot = plt.plot(listx1,list1)
por_plot.ylabel('VOIP')
por_plot.xlabel('Porosity')
por_plot.show()

#plot the heigth graph
hth_plot = plt.plot(listx2,list2)
hth_plot.ylabel('VOIP')
hth_plot.xlabel('Height')
hth_plot.show()

#plot the saturation graph
sat_plot = plt.plot(listx3,list3)
sat_plot.ylabel('VOIP')
sat_plot.xlabel('Saturation')
sat_plot.show()
因此,我使用以下两行代码运行代码:

list_maker(voip1,voip2,voip3,voip4,voip5,voip6,voip7,voip8,voip9,0.3,0.2,0.15,100,150,200,0.8,0.6,0.5)
graph_maker(list_maker)
我得到的错误是:

graph_maker()缺少5个必需的位置参数:“listx2”, “listx3”、“list1”、“list2”和“list3”

据我所知,看起来list_maker()实际上只返回一个列表,显然graph_maker函数需要6个参数。有什么想法吗


谢谢你的帮助

Marco,当您将
list\u-maker
传递到
graph\u-maker
中时,实际上并不是将函数的结果(您想要的列表)作为输入传递到graph\u-maker中,而是将函数传递到它中

但这不是一个简单的问题:

result = list_maker(voip1,voip2,voip3,voip4,voip5,voip6,voip7,voip8,voip9,0.3,0.2,0.15,100,150,200,0.8,0.6,0.5)
graph_maker(result)
由于函数list_maker返回一个包含所有列表的元组,因此需要按以下方式展开它们:

result = list_maker(voip1,voip2,voip3,voip4,voip5,voip6,voip7,voip8,voip9,0.3,0.2,0.15,100,150,200,0.8,0.6,0.5)
graph_maker(*result)

星号将元组扩展为函数所需的5个参数,这有意义吗?

外部函数调用中缺少内部函数调用的一部分:

graph_maker(list_maker)
graph_maker(*list_maker(vars))
或者将初始函数调用分配给一个变量,并使用
*
解包这些值(归功于@zondo)


你需要使用
*x
,就像勒斯特的回答一样。否则,它将传递一个元组,而不是6个参数。首先,您需要传递列表,而不是函数,其次,您需要使用星号“*”运算符解压缩列表<代码>图形生成器(*列表生成器(voip1,voip2,…)
x=list_maker(voip1,voip2,voip3,voip4,voip5,voip6,voip7,voip8,voip9,0.3,0.2,0.15,100,150,200,0.8,0.6,0.5)
graph_maker(*x)