擅长:python、mysql、java
<p>我就是这样做的。</p>
<p>在代码的第一部分,我只是使用了他们给我们的提示。</p>
<p>在<strong>第4章</strong>/<strong>练习项目</strong>/<strong>字符图片网格</strong>中,我们学习了如何“旋转”,然后打印列表列表。这对我的代码的第二部分很有用。</p>
<pre><code>#!/usr/bin/python3
# you can think of x and y as coordinates
tableData = [['apples', 'oranges', 'cherries', 'banana'],
['Alice', 'Bob', 'Carol', 'David'],
['dogs', 'cats', 'moose', 'goose']]
def printTable(table):
# create a new list of 3 "0" values: one for each list in tableData
colWidths = [0] * len(table)
# search for the longest string in each list of tableData
# and put the numbers of characters in the new list
for y in range(len(table)):
for x in table[y]:
if colWidths[y] < len(x):
colWidths[y] = len(x)
# "rotate" and print the list of lists
for x in range(len(table[0])) :
for y in range(len(table)) :
print(table[y][x].rjust(colWidths[y]), end = ' ')
print()
x += 1
printTable(tableData)
</code></pre>