[英]How to print lists within a list in separate columns using for loops?
The problem is from Chapter 6 of ATBS.问题出自 ATBS 的第 6 章。 It is to create a function that is passed a list of lists and then print each list but justified so that each column is neat and flush regardless of the length of the strings within the list.
它是创建一个函数,该函数传递一个列表列表,然后打印每个列表但要对齐,以便无论列表中字符串的长度如何,每一列都是整齐和齐平的。
I created an empty list with the same number of elements as each embedded list (assuming all same length) and found the maximum string length within each list and this number to the empty list.我创建了一个空列表,其元素数量与每个嵌入列表(假设所有长度相同)的元素数量相同,并找到了每个列表中的最大字符串长度,并将此数字添加到空列表中。 Then called on to print each list justified by the maximum string length.
然后调用打印由最大字符串长度对齐的每个列表。
table = [['Tom','Dick','Harry','John'],
['Apples','Oranges','Strawberries','Grapes'],
['Brocolli', 'Asparagus', 'Carrots', 'Potatoes']]
def printTable(tableData):
colWidths = [0] * len(tableData)
for i in range(len(tableData)):
colWidths[i] = max(len(j) for j in tableData[i])
for i in range(len(tableData)):
for j in tableData[i]:
print(j.rjust(colWidths[i]))
printTable(table)
This prints the following这将打印以下内容
Tom
Dick
Harry
John
Apples
Oranges
Strawberries
Grapes
Brocolli
Asparagus
Carrots
Potatoes
but i want it in three separate columns as opposed to just one.但我希望它在三个单独的列中而不是一个。 I know this can simply be done with zip but I haven't covered that yet.
我知道这可以简单地用 zip 来完成,但我还没有涉及。 I want to understand how to do it using for loops before covering new content just to get the job done as I don't feel that will help me learn the fundamentals properly.
我想了解如何在覆盖新内容之前使用 for 循环来完成工作,因为我认为这不会帮助我正确学习基础知识。
try this:尝试这个:
table = [['Tom','Dick','Harry','John'],
['Apples','Oranges','Strawberries','Grapes'],
['Brocolli', 'Asparagus', 'Carrots', 'Potatoes']]
def printTable(tableData):
colWidths = [0] * len(tableData)
for i in range(len(tableData)):
colWidths[i] = max(len(j) for j in tableData[i])
for i in range(len(tableData)):
for j in tableData[i]:
print(j.rjust(sum(colWidths[:i+1])))
printTable(table)
all I did was change rjust
to justify not just for the current column, but for all previous columns as well.我所做的只是更改
rjust
以证明不仅适用于当前列,还适用于所有以前的列。
good thing you already put all widths in a list :)好在你已经把所有的宽度都放在了一个列表中:)
Here's something to get started: the first row (let's call it row0
because it's the one with index 0) is这里有一些开始:第一行(我们称之为
row0
因为它是索引为 0 的row0
行)是
row0 = [x[0] for x in table]
For each list x
in the table you take the first element.对于表中的每个列表
x
,您取第一个元素。
Construct the second row similarly.类似地构造第二行。
To get all rows put this in a loop for i in range(4)
要获取所有行,请将其放入循环中
for i in range(4)
Note that if one of the elements in table
has a different length you have to take care of that somehow, but in your example all elements in table
have the same length 4
.请注意,如果
table
中的元素之一具有不同的长度,您必须以某种方式处理它,但在您的示例中, table
中的所有元素都具有相同的长度4
。
table = [['Tom','Dick','Harry','John'],
['Apples','Oranges','Strawberries','Grapes'],
['Brocolli', 'Asparagus', 'Carrots', 'Potatoes']]
def printTable(tableData):
buffer = []
colWidths = [0] * len(tableData)
for i in range(len(tableData)):
colWidths[i] = max(len(j) for j in tableData[i])
for j in range(len(tableData[i])):
buffer.append("") #filling the buffer array with empty string could have used buffer = ["" for i in range(len(tableData[i]]))]
for i in range(len(tableData)):
for j in range(len(tableData[i])):
buffer[j] += (tableData[i][j].rjust(colWidths[i]))
buffer[j] += " "
for i in range(len(buffer)):
print(buffer[i])
printTable(table)
All I did was to define a buffer array to store each row, after doing that I've created another for loop to print them.我所做的就是定义一个缓冲区数组来存储每一行,然后我创建了另一个 for 循环来打印它们。 When you print a line you can't go back to that line and add a new element so you have to create the line you want to print first, then print it.
当您打印一行时,您无法返回该行并添加新元素,因此您必须先创建要打印的行,然后再打印。 Since the output that you wanted has an item from each list, I've concatenated them into a string and separated them with a space character.
由于您想要的输出在每个列表中都有一个项目,因此我将它们连接成一个字符串并用空格字符分隔它们。
The output is输出是
Tom Apples Brocolli
Dick Oranges Asparagus
Harry Strawberries Carrots
John Grapes Potatoes
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.