Python Form Printing

Posted by archbeta on Thu, 10 Oct 2019 05:12:58 +0200

Python programming is a quick way to practice project topics. Welcome to identify and optimize!
Write a function called printTable(), which accepts a list of strings and displays it in groups
In well-woven tables, each column is right-aligned. Assume that all inner lists contain the same number of strings. For example,
The value may look like this:
tableData = [['apples', 'oranges', 'cherries', 'banana'],
['Alice', 'Bob', 'Carol', 'David'],
['dogs', 'cats', 'moose', 'goose']]
Your printTable() function will print out:

Idea 1:
1. Calculate the length of the longest element in the list (including the internal list);
2. Print lists with the longest element length values as globally right-aligned values
Code:

import copy
tableData = [['apples', 'oranges', 'cherries', 'banana'],
['Alice', 'Bob1111111111111', 'Carol', 'David'],
['dogs', 'cats', 'moose', 'goose']]
def count_width(the_list):
    #Definition function: Calculates the longest value of a list string
    new_list=copy.deepcopy(the_list)
    #Duplicate lists are saved to separate new lists
    colWidths = [0] * len(new_list)
    #Create a list with the same number as tableData
    i=0
    while i < len(new_list):
        new_list[i].sort(key = lambda i:len(i),reverse = True)
        '''Re-order character length in reverse order(From big to small),lamba Represents an anonymous function. key = lambda i:len(i)representative
           //Comparing the len() value of element i
        '''
        colWidths[i]=new_list[i][0]
      #  print (colWidths[i])
        i=i+1
    #Sort tableData[i] in descending order, take the maximum (first), and get a list of the longest strings in each inner list.
    colWidths.sort(key = lambda i:len(i),reverse = True)
    width=len(colWidths[0])
    #Sort colWidths in descending order, take the maximum (first) and calculate their character widths
    #print (width)
    #print (the_list)
    #print (new_list)
    return width

def list_rjust(the_list,width):
    for j in range (len(the_list[0])):
        for i in range (len(the_list)):
            print(the_list[i][j].rjust(width),end=" ")
        print("\r")
list_rjust(tableData,count_width(tableData))

Idea 2:
1. Calculate the value of the longest element in the list (array, no nested array);
2. Print the list according to the value of the longest element in the list (the longest value of each column may be different)
Code:

tableDate=[['apples', 'oranges', 'cherries', 'banana'],
           ['Alice', 'Bob', 'Carol', 'David'],
           ['dogs', 'cats', 'moose', 'goose']]
def findmaxlen(Dates):
    '''
    //Calculate the length of the longest element in an array
    '''
    maxlen=0
    for i in range(len(Dates)):
        if len(Dates[i])>maxlen:
            maxlen=len(Dates[i])
    return maxlen
#print(findmaxlen(tableDate[0]))

def printTable(the_list):
    for j in range (len(the_list[0])):#Print the j th of the internal array
        for i in range (len(the_list)):#Print the first of the arrays
            print(the_list[i][j].rjust(findmaxlen(the_list[i])),end=' ')
            #When printing the j th inner array of the i-th array, align right according to the longest value of the element in the i-th array
        print("\r")
printTable(tableDate)

Topics: Python Lambda Programming