简体   繁体   English

插值lambda难以理解

[英]Interpolation lambda hard to understand

poly1 and poly2 are both lists containing coordinates (x,y). poly1poly2都是包含坐标(x,y)的列表。

Now this function is pre-implemented, which I find hard to understand: 现在此功能已预先实现,我很难理解:

def interpolate():
    # This function returns a list of tuples (x, y).
    return map(lambda a, b: (a[0] + b[0], a[1] + b[1]), poly_1,
               [(time * x[0], time * x[1]) for x in map(lambda p, q: (q[0] - p[0], q[1] - p[1]), poly_1, poly_2)])

But can you actually explain what happens in interpolate ? 但是,您实际上可以解释interpolate会发生什么吗?

All I can extract from this is (as far as the lambdas go) 我能从中提取的是(就lambda而言)

def func_a(a, b):
    return (a[0] + b[0], a[1] + b[1])

and

def func_b(p, q):
    return (q[0] - p[0], q[1] - p[1]), poly_1, poly_2)

But all that mapping is confusing to me. 但是所有这些映射使我感到困惑。

map simply apply a function to every element in one or more arrays map只需将一个函数应用于一个或多个数组中的每个元素

Lets take a look: 让我们来看看:

return map(lambda a, b: (a[0] + b[0], a[1] + b[1]), poly_1,
           [(time * x[0], time * x[1]) for x in map(lambda p, q: (q[0] - p[0], q[1] - p[1]), poly_1, poly_2)])

Its a map function that takes 3 arguments: 它的map函数需要3个参数:

  1. lambda a, b: (a[0] + b[0], a[1] + b[1]) can be translated as lambda a, b: (a[0] + b[0], a[1] + b[1])可以转换为

def foo(a,b) : return (a[0] + b[0], a[1] + b[1])

You will apply it on next two lists 您将在接下来的两个列表中应用它

  1. The first list poly_1 第一个清单poly_1

  2. The second list [(time * x[0], time * x[1]) for x in map(lambda p, q: (q[0] - p[0], q[1] - p[1]), poly_1, poly_2)] : [(time * x[0], time * x[1]) for x in map(lambda p, q: (q[0] - p[0], q[1] - p[1]), poly_1, poly_2)]的第二个列表[(time * x[0], time * x[1]) for x in map(lambda p, q: (q[0] - p[0], q[1] - p[1]), poly_1, poly_2)]

First its a list of tuples with two arguments time * x[0], time * x[1] 首先是一个带有两个参数的元组列表time * x[0], time * x[1]

"x" is taken from the list that return from map(lambda p, q: (q[0] - p[0], q[1] - p[1]), poly_1, poly_2) map(lambda p, q: (q[0] - p[0], q[1] - p[1]), poly_1, poly_2)返回的列表中map(lambda p, q: (q[0] - p[0], q[1] - p[1]), poly_1, poly_2) “ x” map(lambda p, q: (q[0] - p[0], q[1] - p[1]), poly_1, poly_2)

Lets translate it: 让我们翻译一下:

  1. map applies the lambda function to every element of poly_1 and poly_2 映射将lambda函数应用于poly_1和poly_2的每个元素
  2. lambda p, q: (q[0] - p[0], q[1] - p[1]) equals to lambda p, q: (q[0] - p[0], q[1] - p[1])等于

def foo2(p,q) : return (q[0] - p[0], q[1] - p[1])

side note: 边注:

def interpolate(poly_1, poly_2):
    return map(lambda p, q: (p[0] + t*(q[0]-p[0]), p[1] + t*(q[1]-p[1])), poly_1, poly_2)

does the same job. 做同样的工作。 way faster 更快

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM