题目:
编写一个名为 printTable()的函数,它接受字符串的列表的列表,将它显示在组
织良好的表格中,每列右对齐。假定所有内层列表都包含同样数目的字符串。例如,
该值可能看起来像这样:
tableData = [['apples', 'oranges', 'cherries', 'banana'],
['Alice', 'Bob', 'Carol', 'David'],
['dogs', 'cats', 'moose', 'goose']]
你的 printTable()函数将打印出:
apples Alice dog
oranges Bob cats
cherries Carol moose
banana David goose
# 获取一维列表中字符串法的最大长度
def maxlen(tableData):
num = 0
for i in range(len(tableData)):
if num < len(tableData[i]):
num = len(tableData[i])
# 返回列表中最长的字符串的长度
return num
# 二位列表将列表以行格式化打印
# apples oranges cherries banana
# Alice Bob Carol David
# dogs cats moose goose
def printRowTable(tableData,flag):
colWidths = [0] * len(tableData)
for i in range(len(tableData)):
colWidths[i] = maxlen(tableData[i])
num = max(colWidths)
for i in range(len(tableData)):
for j in range(len(tableData[i])):
if flag == 'left' :
print(tableData[i][j].ljust(num + 4), end="")
elif flag == 'right':
print(tableData[i][j].rjust(num + 4), end="")
else:
print(tableData[i][j].center(num + 4), end="")
print()
# 二位列表将列表以列格式化打印
# apples Alice dog
# oranges Bob cats
# cherries Carol moose
# banana David goose
def printColumnTable(tableData,flag):
colWidths = [0] * len(tableData)
for i in range(len(tableData)):
colWidths[i] = maxlen(tableData[i])
num = max(colWidths)
for j in range(len(tableData[0])):
for i in range(len(tableData)):
if flag == 'left' :
print(tableData[i][j].ljust(num + 4), end="")
elif flag == 'right':
print(tableData[i][j].rjust(num + 4), end="")
else:
print(tableData[i][j].center(num + 4), end="")
print()
tableData = [['apples', 'oranges', 'cherries', 'banana'],
['Alice', 'Bob', 'Carol', 'David'],
['dogs', 'cats', 'moose', 'goose']]
print("======================Row===========================")
printRowTable(tableData,'right')
print("====================================================")
printRowTable(tableData,'left')
print("====================================================")
printRowTable(tableData,'center')
print("======================Column========================")
printColumnTable(tableData,'right')
print("====================================================")
printColumnTable(tableData,'left')
print("====================================================")
printColumnTable(tableData,'center')
运行结果:
======================Row===========================
apples oranges cherries banana
Alice Bob Carol David
dogs cats moose goose
====================================================
apples oranges cherries banana
Alice Bob Carol David
dogs cats moose goose
====================================================
apples oranges cherries banana
Alice Bob Carol David
dogs cats moose goose
======================Column========================
apples Alice dogs
oranges Bob cats
cherries Carol moose
banana David goose
====================================================
apples Alice dogs
oranges Bob cats
cherries Carol moose
banana David goose
====================================================
apples Alice dogs
oranges Bob cats
cherries Carol moose
banana David goose