4

练习题要求我将 tabledata 中的嵌套列表排列成右对齐列,每个列都有一个嵌套列表。我已经想出了如何将每列中的每个单词正确对齐到列本身的正确长度,但我不知道如何将列连接到列表中,而不是仅仅将它们打印为一列。

for我已经尝试通过提前迭代来连接 -loop 中的字符串( print(list[i]+list[i+1]+list[i+2]...etc),但是index out of range当它再次循环并递增i.

tableData = [['apples', 'oranges', 'cherries', 'banana'],
             ['Alice', 'Bob', 'Carol', 'David'],
             ['dogs', 'cats', 'moose', 'goose']]

def longlen(strings):
    maximum = 0
    for s in strings:
        if len(s) > maximum:
            maximum = len(s)
    return(maximum)

def printTable(table):
    column_width = [0] * len(table)
    for i in range(len(table)):
        column_width[i] =longlen(table[i])
        for x in range(len(table[i])):
            print(table[i][x].rjust(column_width[i]))

printTable(tableData)

现在我得到了每个单词的正确右对齐,但我不知道如何在一个 4 高 x 3 宽的右对齐表格中输出它。

现在它看起来像这样:

  apples
 oranges
cherries
  banana
Alice
  Bob
Carol
David
 dogs
 cats
moose
goose

我需要这个:

apples   Alice  dogs
oranges  Bob    cats
cherries Carol  moose
banana   David  goose
4

3 回答 3

1

您可以只修改您的printTable()功能:

def printTable(table):
    column_width = [0] * len(table)
    for i in range(len(table)):
        column_width[i] =longlen(table[i])
    for x in range(len(table[0])):
        for i in range(len(table)):
            print(table[i][x].rjust(column_width[i]),end=' ')
        print()

主要的变化是我适当地改变了你的xi位置,以便你打印列表的“转置”。

于 2020-06-30T18:34:26.190 回答
0

像这样的东西应该可以工作。只是为了好玩,我添加了一两个鸭子,该函数将输出一个空白项,其中列的长度不同

tableData = [['apples', 'oranges', 'cherries', 'banana'],
             ['Alice', 'Bob', 'Carol', 'David'],
             ['dogs', 'cats', 'moose', 'duck', 'duck', 'goose']]

def printTable(table):
    # Get length of longest item in each column
    column_widths = [len(max(column, key=len)) for column in table]
    # Get longest column
    longest_col = len(max(table, key=len))
    # Major loop on each item in each nested list
    for j in range(longest_col):
        line = []
        # Nested loop over each nested list
        for i in range(len(table)):
            if j < len(table[i]):
                line.append(table[i][j].rjust(column_widths[i]))
            else:
                line.append(' '*column_widths[i])
        print('  '.join(line))

printTable(tableData)

输出:

  apples  Alice   dogs
 oranges    Bob   cats
cherries  Carol  moose
  banana  David   duck
                  duck
                 goose
于 2019-10-08T23:49:46.820 回答
0

您可以分两步完成:

  1. 遍历每一行并使用内置的字符串方法用空格字符右对齐每个单词,rjust()以使所有单词的长度都与最长的单词相同。为此,您需要首先确定行中最长的单词,这可以使用max()map()函数的组合来完成。

  2. 使用内置zip()函数转置表格的行和列并打印其中的每一行。

请注意,正如当前编写的那样,代码更改了table. 如果你想避免这种情况,请先复制它。

tableData = [['apples', 'oranges', 'cherries', 'banana'],
             ['Alice', 'Bob', 'Carol', 'David'],
             ['dogs', 'cats', 'moose', 'goose']]

def printTable(table):
    for row in table:
        colwidth = max(map(len, row))
        row[:] = [word.rjust(colwidth) for word in row]

    for row in zip(*table):
        print(' '.join(row))


printTable(tableData)
于 2019-10-08T23:50:02.947 回答