Python中的“嵌套”匿名函数/函数句柄

时间:2016-11-02 19:42:44

标签: python matlab python-2.7 lambda anonymous-function

这些天在我的Python上工作,我想从几年前我做过的Python课程中输入一个简短的Matlab代码,但老实说我无法弄清楚是否可以在同一篇文章中完成它方式,如果是的话,怎么做。

我在Python中苦苦挣扎的重要部分如下:

nk = @(x)1
for l=1:3 % calculate basis
    nk = @(x)(nk(x).*(x-1));
end

代码如何在Matlab中运行:

nk = @(x)1创建一个函数句柄nk(x),可由nk(xi)调用,例如xi=[1,2,3,4]x,但截至目前它只会返回1,因为它还不依赖于变量nk(x)

在for循环中,(x-1)每次迭代都会乘以x(此处nk = (x-1)*(x-1)*(x-1)被视为“符号”变量,或称之为“变量”,类似于您的方式定义一个lambda函数),最后它应该是nk(x)

它仍然是一个我可以调用x的函数,其中using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Data.SqlClient; using System.Configuration; namespace IS { public partial class Form2 : Form { public Form2() { InitializeComponent(); LoadTable(); label1.Text = "User: " + User.name + " " + User.surename; } private void label1_Click(object sender, EventArgs e) { } private void LoadTable() { using (SqlConnection connection = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\***\Documents\Visual Studio 2015\Projects\***\Database1.mdf;Integrated Security=True;Connect Timeout=30")) using (SqlCommand query = new SqlCommand("Select FirstName, LastName, Login, Email from Users", connection)) { try { SqlDataAdapter Adapter = new SqlDataAdapter(); Adapter.SelectCommand = query; DataTable UsersTable = new DataTable(); Adapter.Fill(UsersTable); BindingSource UserTableSource = new BindingSource(); UserTableSource.DataSource = UsersTable; dataGridView1.DataSource = UserTableSource; Adapter.Update(UsersTable); } catch(Exception ex) { MessageBox.Show(ex.Message); } } } private void label1_Click_1(object sender, EventArgs e) { } private void UserManageToolStripMenuItem_Click(object sender, EventArgs e) { panel2.Visible = true; } private void button1_Click(object sender, EventArgs e) { Form3 form3 = new Form3(); form3.FormClosing += new FormClosingEventHandler(this.Form3_FormClosing); form3.Show(); } private void button2_Click(object sender, EventArgs e) { LoadTable(); } private void Form3_FormClosing(object sender, FormClosingEventArgs e) { LoadTable(); } private void button3_Click(object sender, EventArgs e) { dataGridView1.ReadOnly = false; button4.Visible = true; button5.Visible = true; } private void button4_Click(object sender, EventArgs e) { try { SqlDataAdapter sda = new SqlDataAdapter(); SqlCommandBuilder scb = new SqlCommandBuilder(); DataTable dt = new DataTable(); scb = new SqlCommandBuilder(sda); sda.Update(dt); dataGridView1.ReadOnly = true; button4.Visible = false; button5.Visible = false; } catch(Exception ex) { MessageBox.Show(ex.Message); } } private void button5_Click(object sender, EventArgs e) { LoadTable(); button4.Visible = false; button5.Visible = false; } } } 是一个数组,其中包含我想要的任何值。

1 个答案:

答案 0 :(得分:0)

以下分段线性插值的粗略示例可能会给您一个想法:

def interpolate(xs,ys):
    #assumes that xs and ys are same-length lists
    #of real numbers, with xs sorted
    def N(x):
        if x < min(xs) or x > max(xs): return False #we don't extrapolate
        if x == max(xs): return ys[-1]
        i = 0
        while xs[i] <= x:
            i+= 1
        m = (ys[i]-ys[i-1])/(xs[i]-xs[i-1])
        return ys[i-1] + m*(x - xs[i-1])
    return N

例如,

>>> f = interpolate([0,1,2],[1,2,4])
>>> f(0)
1.0
>>> f(0.7)
1.7
>>> f(1.9)
3.8
>>> f(2)
4
>>> f(2.1)
False

关键概念是闭包。请参阅this博客文章,以获得精彩的讨论(尤其是讨论函数工厂的部分)。

顺便说一句,您可以使用匿名函数创建闭包,但结果代码不是很易读:

>>> f = (lambda x: (lambda y: x+y))
>>> g = f(3)
>>> g(2)
5

既然你提到了函数“handle”,我认为你真的不想要匿名函数(特别是考虑到你的原始问题有更多的Matlab代码)。