0

我找到了一个函数,它接受一个列表并返回一个字符串。但是我很难理解它到底在做什么?请评论以下代码:

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)
4

1 回答 1

2

一个简短的解释map使我不必在下面的代码中乱扔注释:

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

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

与三个或更多参数一起使用,map假定第一个参数之后的所有参数都是可迭代的,并并行迭代它们,将每个可迭代的第 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。如上所述,使用多个参数,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) 中返回的列表的所有元素调用 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)
于 2012-11-14T09:58:18.327 回答