【问题标题】:Running the same code but with two different datasets (inputs)运行相同的代码,但使用两个不同的数据集(输入)
【发布时间】:2020-10-05 15:30:58
【问题描述】:

我在 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_generating_function() 并生成数据集。然后我会运行一个版本的function1() 和它下面的所有函数,然后我会运行另一个版本的function1() 和它下面的所有其他函数。

什么是实现这一点的好方法?我显然可以复制代码并更改一些函数名称,我也可以将它们全部放入一个单元格并创建一个 for 循环。但是,有没有更好的方法可以理想地保留多个单元格?

谢谢

【问题讨论】:

    标签: python function jupyter-notebook jupyter-lab


    【解决方案1】:

    只需迭代您对第一个函数的两个选择:

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

    【讨论】:

      【解决方案2】:

      抱歉,如果我误解了这个问题,但您能不能做到以下几点:

      单元格 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)
      

      此解决方案假定:

      • 你用返回值定义函数
      • 传递结果并不“昂贵”

      如果不是后者,您也可以将结果保存在文件中。

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

      【讨论】:

        【解决方案3】:

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

        def function1(isFirstTime):
          if isFirstTime:
            # do stuff the first time
            pass
          else:
            # do stuff the second time
            pass
        

        然后您可以迭代函数:

        data_generating_function()
        for b in (True, False):
          function1(b)
          function2()
          function3()
          # ...
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-09-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-10-04
          • 1970-01-01
          • 2018-10-10
          • 1970-01-01
          相关资源
          最近更新 更多