Python 运行相同的代码,但使用两个不同的数据集(输入)

Python 运行相同的代码,但使用两个不同的数据集(输入),python,function,jupyter-notebook,jupyter-lab,Python,Function,Jupyter Notebook,Jupyter Lab,我在JupyterLab中有一个代码,它由分布在几个单元格中的几个函数组成。第一个函数生成一个数据集,该数据集在它之后的所有其他函数中使用 我要做的是运行相同的代码两次,但修改其中一个函数。所以看起来是这样的: data_generating_function() # this function should only be ran once so it generates the same dataset for both trials function_1() # this is the

我在JupyterLab中有一个代码,它由分布在几个单元格中的几个函数组成。第一个函数生成一个数据集,该数据集在它之后的所有其他函数中使用

我要做的是运行相同的代码两次,但修改其中一个函数。所以看起来是这样的:

data_generating_function() # this function should only be ran once so it generates the same dataset for both trials 
function_1() # this is the function that is to be modified once, so there are two version of this function
function_2() # this function and all functions below it stay the same but should be ran twice
function_3()
function_4()
function_5()
因此,我将运行一次
data\u generating\u function()
,并生成数据集。然后我将运行一个版本的
function1()
及其下面的所有函数,然后运行另一个版本的
function1()
及其下面的所有其他函数

实现这一点的好方法是什么?很明显,我可以复制代码,只需更改一些函数名,也可以将其全部放在一个单元格中并创建for循环。然而,有没有更好的方法可以理想地保存多个细胞呢


谢谢您

只需对第一个函数的两个选项进行迭代:

data_generating_function() 
for func1 in (function1a, function1b):
    func1()
    function_2()
    function_3()
    function_4()
    function_5()

只需对第一个函数的两个选项进行迭代:

data_generating_function() 
for func1 in (function1a, function1b):
    func1()
    function_2()
    function_3()
    function_4()
    function_5()

尽可能避免修改或直接迭代函数。在这种情况下,最好是向
function1
添加一个布尔参数,指定要运行的函数版本。它看起来像这样:

data_generating_function() # this function should only be ran once so it generates the same dataset for both trials 
function_1() # this is the function that is to be modified once, so there are two version of this function
function_2() # this function and all functions below it stay the same but should be ran twice
function_3()
function_4()
function_5()
def功能1(isFirstTime):
如果是第一次:
#第一次做事
通过
其他:
#第二次做事
通过
然后可以迭代函数:

数据生成函数()
对于b in(真、假):
职能1(b)
职能2()
职能3()
# ...

尽可能避免修改或直接迭代函数。在这种情况下,最好是向
function1
添加一个布尔参数,指定要运行的函数版本。它看起来像这样:

data_generating_function() # this function should only be ran once so it generates the same dataset for both trials 
function_1() # this is the function that is to be modified once, so there are two version of this function
function_2() # this function and all functions below it stay the same but should be ran twice
function_3()
function_4()
function_5()
def功能1(isFirstTime):
如果是第一次:
#第一次做事
通过
其他:
#第二次做事
通过
然后可以迭代函数:

数据生成函数()
对于b in(真、假):
职能1(b)
职能2()
职能3()
# ...

如果我误解了这个问题,我深表歉意,但您能否做到以下几点:

单元1:

# define all functions
单元2:

dataset = data_generating_function()
单元3:

# Run version 1 of function 1 on dataset
result_1_1 = function_1_v1(dataset)
result_2_1 = function_2(result_1_1)
result_3_1 = function_3(result_2_1)
function_4(result_3_1)
第4单元:

# Run version 2 of function 1 on dataset
result_1_2 = function_1_v2(dataset)
result_2_2 = function_2(result_1_2)
result_3_2 = function_3(result_2_2)
function_4(result_3_2)
此解决方案假定:

  • 您可以使用返回值定义函数
  • 传递结果并不“昂贵”
如果文件不是这样,也可以将结果持久化到文件中


为了减少函数_1中的代码重复,您可以添加一个在两个版本之间切换的参数。

如果我误解了这个问题,请道歉,但您不能执行以下操作:

单元1:

# define all functions
单元2:

dataset = data_generating_function()
单元3:

# Run version 1 of function 1 on dataset
result_1_1 = function_1_v1(dataset)
result_2_1 = function_2(result_1_1)
result_3_1 = function_3(result_2_1)
function_4(result_3_1)
第4单元:

# Run version 2 of function 1 on dataset
result_1_2 = function_1_v2(dataset)
result_2_2 = function_2(result_1_2)
result_3_2 = function_3(result_2_2)
function_4(result_3_2)
此解决方案假定:

  • 您可以使用返回值定义函数
  • 传递结果并不“昂贵”
如果文件不是这样,也可以将结果持久化到文件中

为了减少函数_1中的代码重复,可以添加一个在两个版本之间切换的参数