Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.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_Excel_Pandas_Openpyxl - Fatal编程技术网

Python-以列表项为参数运行函数

Python-以列表项为参数运行函数,python,excel,pandas,openpyxl,Python,Excel,Pandas,Openpyxl,我想运行一个函数列表,其中每个函数都应该从列表中选取一项作为参数 Func1参数取第一个列表值,Func2参数取第二个列表值…依此类推 到目前为止,我有以下几点: main.py 表1.py 表2.py 表3.py 等等 为了使其更直观,代码的外观/工作方式如下: #for first iteration for item in sheet_lst: #take "Sheet1" for f in func_lst: #take s1.func1 f(item) #run fu

我想运行一个函数列表,其中每个函数都应该从列表中选取一项作为参数

Func1参数取第一个列表值,Func2参数取第二个列表值…依此类推

到目前为止,我有以下几点:

main.py

表1.py

表2.py

表3.py

等等

为了使其更直观,代码的外观/工作方式如下:

#for first iteration
for item in sheet_lst:  #take "Sheet1"
for f in func_lst: #take s1.func1
        f(item) #run function with item parameter.

循环的最终结果是:

s1.func1("Sheet1"); 
s2.func2("Sheet2"); 
s3.func3("Sheet3"); 
s4.func4("Sheet4"); 
s5.func5("Sheet5");
有人知道如何构造循环吗

提前谢谢

用于生成您描述的循环

for wbkey, f in zip(sheet_lst, func_lst):
   f(wbkey)
上述结果是:

s1.func1("Sheet1"); 
s2.func2("Sheet2"); 
s3.func3("Sheet3"); 
s4.func4("Sheet4"); 
s5.func5("Sheet5");

不确定目标。给定函数列表
func\u lst
和工作表列表
wbkey
,您是否试图将第一个函数
func\u列表[0]应用于wbkey[0]、func\u列表[1]应用于wbkey[1]、…等
?很抱歉造成混淆。我想从列表中发送项目,作为每个函数的参数。>>s1.功能1(“表1”);>>s2.func2(“表2”);>>s3.功能3(“表3”);>>s4.功能4(“表4”);>>s5.5(“表5”)@Darryg我将编辑问题以使其更清楚。@Darryg出于某种原因,它只会持续到s4.4。保留s5.5。@Raccoon_17--zip将迭代到较短长度的长度。您确定在工作表列表和功能列表中放置了5个元素吗?@DarryIG我添加了一些项目,但两个列表的项目数相同。func_lst有10个函数,sheet_lst在列表中也有10个项目。@Raccoon_17--你得到了什么:
print(list(zip(sheet_lst,func_lst))
。它应该是一个元组列表,每个元组的第一个元素来自sheet_列表,第二个元素来自func_列表。@daryig它终于成功了!你是对的!非常感谢你的帮助!!:)
def sheet3(n): #n param should take "Sheet3"
    print(n*2) #prints "Sheet3Sheet3"

#for first iteration
for item in sheet_lst:  #take "Sheet1"
for f in func_lst: #take s1.func1
        f(item) #run function with item parameter.

s1.func1("Sheet1"); 
s2.func2("Sheet2"); 
s3.func3("Sheet3"); 
s4.func4("Sheet4"); 
s5.func5("Sheet5");
for wbkey, f in zip(sheet_lst, func_lst):
   f(wbkey)
s1.func1("Sheet1"); 
s2.func2("Sheet2"); 
s3.func3("Sheet3"); 
s4.func4("Sheet4"); 
s5.func5("Sheet5");