python代码求解答
求详细解释下中间那段求最大长度的for循环,为什么那样写?还有流程是怎样的?谢谢啦。tableData=[['apple','orage','cherries','ban...
求详细解释下中间那段求最大长度的for循环,为什么那样写?还有流程是怎样的?谢谢啦。
tableData = [['apple', 'orage', 'cherries', 'banana'],
['alice', 'bob', 'carol', 'david'],
['dogs', 'cats', 'moose', 'goose']]
def printTable(tableData):
colWidths = [0] * len(tableData)
print(colWidths)
# 找出每列字符串最大长度
for i in range(len(tableData)):
colWidths[i] = len(tableData[i][0])
print(colWidths)
for j in range(len(tableData[i])):
if colWidths[i] < len(tableData[i][j]):
colWidths[i] = len(tableData[i][j])
# 行列转换
table = list(zip(*tableData))
# 左对齐打印
for i in range(len(table)):
for j in range(len(table[i])):
print(table[i][j].ljust(colWidths[j] + 5, ' '), end="")
print()
printTable(tableData)
打印的结果是:
apple alice dogs
orage bob cats
cherries carol moose
banana david goose 展开
tableData = [['apple', 'orage', 'cherries', 'banana'],
['alice', 'bob', 'carol', 'david'],
['dogs', 'cats', 'moose', 'goose']]
def printTable(tableData):
colWidths = [0] * len(tableData)
print(colWidths)
# 找出每列字符串最大长度
for i in range(len(tableData)):
colWidths[i] = len(tableData[i][0])
print(colWidths)
for j in range(len(tableData[i])):
if colWidths[i] < len(tableData[i][j]):
colWidths[i] = len(tableData[i][j])
# 行列转换
table = list(zip(*tableData))
# 左对齐打印
for i in range(len(table)):
for j in range(len(table[i])):
print(table[i][j].ljust(colWidths[j] + 5, ' '), end="")
print()
printTable(tableData)
打印的结果是:
apple alice dogs
orage bob cats
cherries carol moose
banana david goose 展开
若以下回答无法解决问题,邀请你更新回答
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询