Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8835239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:12:53+00:00 2026-06-14T09:12:53+00:00

I have found a function that takes a list of lists and return a

  • 0

I have found a function that takes a list of lists and return a string.
However I have a hard time understanding exactly what it is doing? Please comment the code below:

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)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T09:12:54+00:00Added an answer on June 14, 2026 at 9:12 am

    A short explanation of map which saves me from having to litter the code below with comments:

    The map function applies its first argument (usually a function, but it can be a class or any other callable thing too) to each value in the second argument and returns the resulting list. Think of it as transforming each element with the given function. Used with two arguments, it works like this:

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

    Used with three or more arguments, map assumes all arguments after the first one are iterables and iterates through them in parallel, passing the nth element of each iterable on to the function on the nth pass:

    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"
    

    A commented version of your code:

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

    Let’s split this line into its parts:

    (1) [map(len, a) for a in arrays]
    

    This maps len to every list in arrays – meaning you get a list of
    lists of lengths of the elements. As an example, for the input [["a","b","c"], ["1","11","111"], ["n", "n^2", "n^10"]] the result would be [[1, 1, 1], [1, 2, 3], [1, 2, 4]].

    (2) map(lambda *x:x, *(1))
    

    The * unwraps the list obtained in (1), meaning each element is
    passed to map as a separate argument. as described above, with
    multiple arguments, map passes a to the function. the lambda
    defined here just returns all of its arguments as a tuple.
    continuing the example above, for input [[1, 1, 1], [1, 2, 3], [1, 2, 4]]
    the result would be [(1, 1, 1), (1, 2, 2), (1, 3, 4)]
    this basically results in a matrix transpose of the input

    (3) [max(value) for value in (2)]
    

    This calls max on all elements of the list returned in (2) (keep in mind the elements are tuples). For input [(1, 1, 1), (1, 2, 2), (1, 3, 4)] the result would be [1, 2, 4].

    So, in the context here, the whole line takes the input array and computes the maximum length of the elements in each column.

    The rest of the code:

        #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)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have done various tests and I have found that the jQuery validate function
I have a function which returns a List<Dictionary<string, object>> where object is a standard
I am making a function that takes in a list of drivers and passengers
I have a subroutine of a Python application that takes a list of eight-character
I have a function that takes 24 bit to 12 bit hex and prints
I have a function called FindSpecificRowValue that takes in a datatable and returns the
I have created a function that reads lists of ID pairs (i.e. [(A,B),(B,C),(C,D),...] and
I'm learning JQuery and have found the factory function $() with its selectors quite
I found a very interesting/strange thing about MAX() function in SQL. I have column
I have found that I have no problem using require to load something like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.