需要了解包含lambda的函数

需要了解包含lambda的函数,lambda,python-2.7,Lambda,Python 2.7,我发现了一个函数,它获取列表列表并返回字符串。 然而,我很难理解它到底在做什么?请对以下代码进行注释: def getAsTable(self, arrays): """ This method takes an array of arrays and returns string (which is really a table) :param arrays: An array of arrays :returns: string (this is really a

我发现了一个函数,它获取列表列表并返回字符串。 然而,我很难理解它到底在做什么?请对以下代码进行注释:

def getAsTable(self, arrays):
    """ This method takes an array of arrays and returns string (which is really a table) 
    :param arrays: An array of arrays 
    :returns: string (this is really a table of the input)

    >>> [[a, b, b], [1, 2, 3], [4, 5, 6]]
    >>> a    b    c
    >>> 1    2    3
    >>> 4    5    6

    """
    def areAllEqual(lst):
        return not lst or [lst[0]] * len(lst) == lst

    if not areAllEqual(map(len, arrays)):
        return "Cannot print a table with unequal array lengths"

    verticalMaxLengths = [max(value) for value in map(lambda * x:x, *[map(len, a) for a in arrays])]
    spacedLines = []

    for array in arrays:
        spacedLine = ''
        for i, field in enumerate(array):
            diff = verticalMaxLengths[i] - len(field)
            spacedLine += field + ' ' * diff + '\t'
        spacedLines.append(spacedLine)

    return '\n  '.join(spacedLines)
一个简短的解释让我不必在下面的代码中添加注释:

map
函数将其第一个参数(通常是函数,但也可以是类或任何其他可调用的对象)应用于第二个参数中的每个值,并返回结果列表。将其视为使用给定函数变换每个元素。与两个参数一起使用时,其工作原理如下:

def map(fct, iterable): return [fct(x) for x in iterable]
与三个或三个以上参数一起使用,
map
假定第一个参数之后的所有参数都是iterable,并并行迭代,将每个iterable的第n个元素传递给第n次传递的函数:

def p(a,b,c): print "a: %s, b:%s, c:%s"
map(p, "abc", "123", "456") #-> prints "a 1 4", then "b 2 5", then "c 3 6"
代码的注释版本:

def getAsTable(self, arrays):

    #helper function checking that all values contained in lst are equal
    def areAllEqual(lst):
        #return true for the empty list, or if a list of len times the first
        #element equals the original list
        return not lst or [lst[0]] * len(lst) == lst

    #check that the length of all lists contained in arrays is equal
    if not areAllEqual(map(len, arrays)):
        #return an error message if this is not the case
        #this should probably throw an exception instead...
        return "Cannot print a table with unequal array lengths"

    verticalMaxLengths = [max(value) for value in map(lambda * x:x, *[map(len, a) for a in arrays])]
让我们把这条线分成几部分:

(1) [map(len, a) for a in arrays]
这会将len映射到数组中的每个列表,这意味着您将得到一个 元素的长度列表。例如,对于输入
[[“a”、“b”、“c”]、[“1”、“11”、“111”]、[“n”、“n^2”、“n^10”]
,结果将是
[[1,1,1]、[1,2,3]、[1,2,4]

(2) map(lambda *x:x, *(1))
*
打开(1)中获得的列表,这意味着每个元素都是 作为单独的参数传递给map。如上所述,具有 如果有多个参数,映射会将一个参数传递给函数。兰姆达酒店 这里定义的只是将其所有参数作为元组返回。 继续上面的示例,对于输入
[[1,1,1],[1,2,3],[1,2,4]
结果将是
[(1,1,1)、(1,2,2)、(1,3,4)]
这基本上导致了输入的矩阵转置

(3) [max(value) for value in (2)]
这将调用(2)中返回的列表中所有元素的max(请记住这些元素是元组)。对于输入
[(1,1,1)、(1,2,2)、(1,3,4)]
结果将是
[1,2,4]

因此,在这里的上下文中,整行接受输入数组并计算每列中元素的最大长度

代码的其余部分:

    #initialize an empty list for the result
    spacedLines = []

    #iterate over all lists
    for array in arrays:
        #initialize the line as an empty string
        spacedLine = ''
        #iterate over the array - enumerate returns position (i) and value
        for i, field in enumerate(array):
            #calculate the difference of the values length to the max length
            #of all elements in the column
            diff = verticalMaxLengths[i] - len(field)
            #append the value, padded with diff times space to reach the
            #max length, and a tab afterwards
            spacedLine += field + ' ' * diff + '\t'
        #append the line to the list of lines
        spacedLines.append(spacedLine)

    #join the list of lines with a newline and return
    return '\n  '.join(spacedLines)