2012-11-14 34 views
0

我找到了一个函数,它接收一个列表并返回一个字符串。 但是,我很难理解它在做什么?请评论下面的代码:需要了解一个函数,其中包括lambdas

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) 

回答

2

map一个简短的说明从而节省了我不必垃圾下面的代码注释:

map功能应用的第一个参数(通常是功能,但能成为一个类或任何其他可调用的东西)到第二个参数中的每个值并返回结果列表。把它看作是用给定函数转换每个元素。使用两个参数,它的工作原理是这样的:

def map(fct, iterable): return [fct(x) for x in iterable] 

使用三个或更多的参数,map假设后的第一个所有参数均为iterables和遍历它们并联,通过每个迭代的第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将a传递给该函数。此处定义的lambda 只是将其所有参数作为元组返回。 继续上面的例子,对于输入[[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)(保持在调用最大的矩阵转置介意元素是元组)。对于输入[(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)