The code is as follows:
The title means that a list of lists is displayed in a well-organized table by a function, with the right alignment of each column
Tabledata = [[' Apples ', ' oranges ', ' cherries ', ' banana '],
[' Alice ', ' Bob ', ' Carol ', ' David '],
[' Dogs ', ' cats ', ' Moose ', ' goose ']
‘‘‘
Apples Alice Dogs
Oranges Bob Cats
Cherries Carol Moose
Banana David Goose
‘‘‘
#输出每一列右对齐 I don't think it should be the last line of string,
#但是书上的不是啊, it bothered me all night.
def printtable (Tabledata):
Colwidths = [0] * Len (tabledata)
col = []
For I in range (0, Len (tabledata[0])):
For j in range (0, Len (colwidths)):
Col.append (Len (tabledata[j][i))
Max_len = max (col)
For I in range (0, Len (tabledata[0])):
For j in range (0, Len (colwidths)):
Print (Tabledata[j][i].rjust (max_len), end= ")
Print ()
if __name__ = = ' __main__ ':
Tabledata = [[' Apples ', ' oranges ', ' cherries ', ' banana '],
[' Alice ', ' Bob ', ' Carol ', ' David '],
[' Dogs ', ' cats ', ' Moose ', ' goose ']
PrintTable (Tabledata)
----------------------------------------------------------------
----------------------------------------------------------------
Apples Alice Dogs
Oranges Bob Cats
Cherries Carol Moose
Banana David Goose
The right alignment of each column can only be so, unexpectedly better solve, if there is God knows how to solve please leave a message
Python Programming Quick Start Sixth Chapter practice Project Reference Code