定义的列表:
tableDate = [['apples','oranges','cherries','banana'],
['Alice','Bob','Carol','David'],
['dogs','cats','moose','goose']]
输出形式:
apples Alice dogs
oranges Bob cats
cherries Carol moose
banana David goose
直接贴代码:
#定义一个表
tableDate = [['apples','oranges','cherries','banana'],
['Alice','Bob','Carol','David'],
['dogs','cats','moose','goose']]
#保存每行最长字符串的长度:
col_len = [len(tableDate[0][0]),len(tableDate[1][0]),len(tableDate[2][0])]
#循环求出每行的最长字符串长度:
for j in range(len(tableDate)):
for i in range(len(tableDate[j])):
if(col_len[j] < len(tableDate[j][i])):
col_len[j] = len(tableDate[j][i])
#保证每行输出为原先的列
for j in range(len(tableDate[0])):
str = ''
for i in range(len(tableDate)):
str = str + tableDate[i][j].rjust(col_len[i]+1)
print(str)