【问题标题】:Call a list of functions with random parameters调用具有随机参数的函数列表
【发布时间】:2018-10-28 21:32:47
【问题描述】:

如何使用随机参数调用一组函数。

import random

def sum_3 (a):
    return (sum(a) + 3)
def sum_2 (b):
    return (sum(b) + 2)

# Call the functions 
functions = [sum_3, sum_2]
# Using random arguments
random.sample(range(1, 100000), 5)

【问题讨论】:

    标签: python function loops random


    【解决方案1】:

    您可以迭代您的函数并使用示例调用它们。

    >>> the_sample = random.sample(range(1, 100000), 5)
    >>> the_sample
    >>> [6163, 38513, 35085, 4876, 27506]
    >>>
    >>> for func in functions:
    ...:    print(func(the_sample))
    ...:    
    112146
    112145
    

    建立一个结果列表:

    >>> [func(the_sample) for func in functions]
    >>> [112146, 112145]
    

    【讨论】:

      【解决方案2】:

      假设functions 是一个函数列表:

      import random
      # Call your function like this:
      random.choice(functions)(a)
      

      如果您更喜欢使用numpy,请使用np.random.choice(我这样做是因为它更像是一个默认的数学库)

      【讨论】:

        【解决方案3】:

        取决于你想调用它多少次。一种方法如下:

        import random
        
        def sum_3 (a):
            print('calling sum3 with', a)
            return (sum(a) + 3)
        
        def sum_2 (b):
            print('calling sum2 with', b)
            return (sum(b) + 2)
        
        functions = [sum_3, sum_2]
        for i in range(3): # call each function 3 times
            for func in functions:
                print(func(random.sample(range(1, 100000), random.randint(2, 6))))
        

        输出

        calling sum3 with [76385, 37776, 19464]
        133628
        calling sum2 with [21520, 97936, 44610]
        164068
        calling sum3 with [1184, 4786]
        5973
        calling sum2 with [2680, 36487, 24265, 39569, 18331]
        121334
        calling sum3 with [87777, 81241, 95238, 58267, 3434, 85015]
        410975
        calling sum2 with [5020, 68999, 50868, 17544]
        142433
        

        【讨论】:

          【解决方案4】:

          我将定义一个通用函数,它允许我选择和预定义我将使用的函数,然后将所选函数与参数一起应用。您可以认为这是一个两步方法:

          1. 将您需要的函数编译到一个对象中。

          2. 将编译对象中的函数应用于参数。

          因此,不要像在问题中那样将函数放在列表中,而是将它们编译到带有另一个函数的对象中。它适用于多个功能以及单个功能的情况,因此非常通用。

          import random
          
          def sum_3 (a):
              return (sum(a) + 3)
          def sum_2 (b):
              return (sum(b) + 2)
          
          parameters = random.sample(range(1, 100000),5)
          
          # generic function to apply functions on parameters
          def compile(*fs): return lambda x: map(lambda f: f(x), fs)
          
          # choose the functions you want to apply
          dosum3and2 = compile(sum_3,sum_2)
          dosum2only = compile(sum_2)
          
          # apply the defined functions to parameters
          print(list(dosum3and2(parameters)))
          print(list(dosum2only(parameters)))
          # output =>
          # [294334, 294333]
          # [294333]
          

          当你有很多函数sum_1sum_2sum_3、...、sum_n时,你先编译它们,然后应用到像这样的参数

          # compile the chosen functions
          # which looks very similar as what you did with list (functions = [sum_3, sum_2]).
          functions = compile(sum_1,sum_2,sum_3,...,sum_n)
          # apply to parameters
          functions(parameters)
          

          【讨论】:

            猜你喜欢
            • 2015-03-02
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-12-27
            • 2012-08-02
            • 2019-07-01
            • 2022-11-12
            • 1970-01-01
            相关资源
            最近更新 更多