如何 运行 将单个函数输入到 python 中的多次

How to run single function in to a number of times in python

我尝试使用下面的代码 运行 简单函数 n 次:

 df = pd.DataFrame()

def repeat_fun(times, f, args):
    for i in range(times): f(args)

def f(x):
    g = np.random.normal(0, 1, 32)
    mm = np.random.normal(491.22, 128.23, 32)
    x = 491.22+(0.557*(mm -491.22))+(g*128.23*(np.sqrt(1-0.557**2)))
    print x
repeat_fun(2,f,df)

但我想要关于 运行 次的列式结果。上面的函数在一个数组中给出了结果 types.Can 谁能帮我解决这个问题。

很难理解您的意思,但我假设您希望 f 的结果作为列存储在数据框中。如果是这样的话:

import pandas as pd
import numpy as np
df = pd.DataFrame()

def repeat_fun(times, f, args):
    for i in range(times): f(i,args)

def f(iteration,df):
    g = np.random.normal(0, 1, 32)
    mm = np.random.normal(491.22, 128.23, 32)
    x = 491.22+(0.557*(mm -491.22))+(g*128.23*(np.sqrt(1-0.557**2)))
    df[iteration] = x

repeat_fun(2,f,df)

运行 这个并查看 at/print df 的内容,看看是否有帮助。