我关注http://automatetheboringstuff.com/chapter6/
在页面的最底部是关于格式化表格的练习。
这是我的代码:
tableData = [['apples', 'oranges', 'cherries', 'banana'],
['Alice', 'Bob', 'Carol', 'David'],
['dogs', 'cats', 'moose', 'goose']]
def printTable(table):
colWidths = [0] * len(table)
for line in table:
max = 0
for word in line:
if len(word) > max:
max = len(word)
colWidths[table.index(line)] = max
for a in range(len(table)-2):
for b in range(len(table[0])):
print(table[a][b].rjust(colWidths[0])+table[a+1][b].rjust(colWidths[1])+table[a+2][b].rjust(colWidths[2]))
"""
print(table[0][0].rjust(colWidths[0]), table[1][0].rjust(colWidths[1]), table[2][0].rjust(colWidths[2]))
print(table[0][1].rjust(colWidths[0]), table[1][1].rjust(colWidths[1]), table[2][1].rjust(colWidths[2]))
print(table[0][2].rjust(colWidths[0]), table[1][2].rjust(colWidths[1]), table[2][2].rjust(colWidths[2]))
print(table[0][3].rjust(colWidths[0]), table[1][3].rjust(colWidths[1]), table[2][3].rjust(colWidths[2]))
"""
print()
printTable(tableData)
注释掉的行格式化了应有的一切。实际的代码没有。为了正确格式化,我需要为每列添加1到.rjust()
(例如,我需要100列.rjust(colWidths[1]+99)
)。
当我手动打印时,为什么会出现这种情况呢?
答案 0 :(得分:2)
for循环中的print语句使用字符串连接:
print(table[a][b].rjust(colWidths[0])+table[a+1][b].rjust(colWidths[1])+table[a+2][b].rjust(colWidths[2]))
连接在内存中创建每个字符串,然后在新字符串中将它们组合在一起。不会在项目之间添加空格,这就是为什么你需要在它之前为每一列添加一个字符。
代码中注释掉的行使用逗号分隔参数:
"""
print(table[0][0].rjust(colWidths[0]), table[1][0].rjust(colWidths[1]), table[2][0].rjust(colWidths[2]))
print(table[0][1].rjust(colWidths[0]), table[1][1].rjust(colWidths[1]), table[2][1].rjust(colWidths[2]))
print(table[0][2].rjust(colWidths[0]), table[1][2].rjust(colWidths[1]), table[2][2].rjust(colWidths[2]))
print(table[0][3].rjust(colWidths[0]), table[1][3].rjust(colWidths[1]), table[2][3].rjust(colWidths[2]))
"""
带有逗号分隔项的print语句,使用空格分隔它们。这可能是您的列正确排列的原因。
This answer更详细地解释了它。
答案 1 :(得分:1)
你所挣扎的代码部分与我和我的工作非常相似。
def printTable(List):
colWidths = [0] * len(tableData)
for line in range(len(tableData)):
for word in range(len(tableData[line])):
if colWidths[line] <= len(tableData[line][word]):
colWidths[line] = len(tableData[line][word])
else:
colWidths[line] = colWidths[line]
#this is the part where you struggled
for li in range(len(tableData[0])):
for i in range(len(tableData)):
print(tableData[i][li].rjust(colWidths[i]), end =" ")
print()
答案 2 :(得分:1)
下面是我用来在表格中找到最长字符串并打印表格的代码
(click)="..."
def printTable(table):
tableData = [['apples', 'orange', 'cherries', 'banana'],
['Alice', 'Bob', 'Carol', 'David'],
['dogs', 'cats', 'moose', 'goose']]
答案 3 :(得分:0)
由于每个单独列表的内容都定义了每列必需的宽度,因此可以在循环中执行len(max(tableData [x]))以获得每列的最大长度。将此附加到列表中,即可轻松传输:
tableData = [['apples', 'oranges', 'cherries', 'banana'],
['Alice', 'Bob', 'Carol', 'David'],
['dogs', 'cats', 'moose', 'goose']]
colWidth = []
def printTable(table):
for x in range(len(table)):
colWidth.append(len(max(table[x],key=len))+1)
for x in range(len(table[0])):
for i in range(len(table)):
print (table [i][x].rjust(colWidth[i]), end = " ")
print()
printTable(tableData)
答案 4 :(得分:0)
这是我的代码,经过一周的烦扰并追逐着我的梦,终于可以正常工作了。
tableData = [['apples', 'oranges', 'cherries', 'banana'],
['Alice', 'Bobolon', 'Carolina', 'Davidovv'],
['dogsee', 'puscats', 'moosara', 'gooseano']]
def printTable(table):
colwidths=[0]*len(table)
for i in range(len(table)):
for j in range(len(table[i])):
if len(table[i][j])>colwidths[i]:
colwidths[i]=len(table[i][j])
for line in range(len(table[0])): #PYTHON PRINTS LINE PER LINE. NOT COLUMN PER COLUMN.ABOUT TABLE[0]: IT IS ESTABLISHED THAT ALL ITEMS IN TABLEDATA WILL HVE THE SAME LENGTH, SO THAT IT DOESNT MATTER WETHER YOU PUT [0], [1] OR [2] BECAUSE THEY ALL HAVE LENGTH OF 4 ITEMS (IN CASE OF THE TABLEDATA LIST
for column in range(len(table)): #THERE ARE AS MANY COLUMNS AS ITEMS(LISTS) IN TABLEDATA
print(table[column][line].ljust(colwidths[column]*2),end=" ") #NOW, WE PRINT THE FIRST WORD OF THE FIRST COLUMN, FOLLOWED BUYT THE FIRST WORD OF THE SECOND COLUMN AND SO ON. .END= HELPS WITH NOT HAVING TO CONCATENATE THESE AND KEEPING ITEMS IN THE SAME LINE.
print() #WITHOUT THIS PRINT (WHICH PRINTS A NEW LINE), ALL ITEMS WOULD BE IN THE SAME LINE, DUE TO THE PREVIOS .END=
printTable(tableData)
答案 5 :(得分:-1)
tableData = [['apples', 'oranges', 'cherries', 'bananas'],
['Alice', 'Bob', 'Caroline', 'David'],
['dogs', 'cats', 'moose', 'goose']]
#To calculate the length of the longest word in the table
colwid = 0
for j in range(len(tableData[0])):
for i in range(len(tableData)):
if colwid < len(tableData[i][j]):
colwid = len(tableData[i][j])
i = i + 1
j = j + 1
#Print the table with each field left justified with column length from above
for j in range(len(tableData[0])):
for i in range(len(tableData)):
print(tableData[i][j].ljust(colwid), end=' ')
i = i + 1
j = j + 1
print()