小编Jor*_*rge的帖子

函数如何接受冒号(范围运算符)作为参数(在 Python 中)?

我想做以下事情:我想定义一个递归函数,funct在该函数中,它自己的最后一个引用返回数组的一个数字temp。问题是funct必须对自身进行积分(请参阅下面的代码),如果funct可以接受冒号:作为参数,这将非常容易。所以,到目前为止我有这个(简化的)代码:

import numpy as np   
import scipy.integrate as ints

temp = np.array([[1,2,3,4,5],[4,0,6,7,8],[7,8,9,10,11],[1,2,3,4,5],[6,7,8,9,10]])

def funct(x,y,n):
    if n>1:
        return (temp[x,y] + ints.cumtrapz(temp[x,:]*funct(:,y,n-1), x=None, dx=1, initial=0)[-1])
    else:
        return temp[x,y]
funct = np.vectorize(funct)
funct(1,1,3)
Run Code Online (Sandbox Code Playgroud)

问题是,funct不能接受结肠:作为参数,它并不重要,如果我想矢量化funct后面的代码。

例如,如果我更改上述代码的这一部分

ints.cumtrapz(temp[x,:]*funct(:,y,n-1), x=None, dx=1, initial=0)[-1]) 
Run Code Online (Sandbox Code Playgroud)

为了

ints.cumtrapz(temp[x,:]*temp[:,y], x=None, dx=1, initial=0)[-1]) 
Run Code Online (Sandbox Code Playgroud)

我没有问题。我只想递归地做最后一部分。

python recurrence arguments function colon

2
推荐指数
1
解决办法
2763
查看次数

标签 统计

arguments ×1

colon ×1

function ×1

python ×1

recurrence ×1