簡體   English   中英

如何在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)

但是我想要關於運行時間的列結果。 上面的函數給出了一種數組類型的結果,誰能幫我解決這個問題。

很難理解您的意思,但是我假設您希望將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)

運行此命令,查看/打印df的內容,看看是否有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM