【问题标题】:Automate the Boring Stuff, Chapter 6 Table Printer自动化无聊的东西,第 6 章 台式打印机
【发布时间】:2015-07-07 00:15:17
【问题描述】:

我关注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))。

当我手动打印时它似乎工作得很好,为什么会发生这种情况?

【问题讨论】:

  • 您是否尝试过像在手动打印语句中那样在 for 循环打印语句中使用逗号分隔表值?更多信息here.

标签: python python-3.x formatting


【解决方案1】:

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]))
"""

打印语句用逗号分隔项目,使用空格分隔它们。这可能就是您的列正确排列的原因。

This answer 更详细地解释了它。

【讨论】:

    【解决方案2】:

    您遇到的代码部分与我的和我的工作非常相似。

    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()
    

    【讨论】:

      【解决方案3】:

      由于每个单独列表的内容定义了每列所需的宽度,您可以在循环中执行 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】:

        下面是我用来在表格中查找最长字符串并打印表格的代码

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

        定义打印表(表):

            strName = ''
            maxLen = -1
            # Find longest string in table
            for y in range(len(table[0])):
                         
                    for x in range(len(table)): 
                           strName = table[x][y]
                           if len(strName) > maxLen:
                           maxLen = len(strName) + 1
        
            # Print table   
            for y in range(len(table[0])):
            
                    newTable = ''           
                    for x in range(len(table)): 
                        newTable = newTable + (table[x][y].ljust(maxLen))                 
                    print(newTable)
                          
        
        printTable(tableData) # Call function
        

        【讨论】:

          【解决方案5】:

          这是我的代码,在困扰我一周并在梦中追逐我之后终于可以工作了:

          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)
          

          【讨论】:

            【解决方案6】:

            这是我的解决方案。我有一个更简单的函数适用于给定的数据(3 列,每列 4 行),但后来我开始想知道如果输入一个具有不同长度行的二维数组会发生什么,所以我做了一些调整以适应这些情况。

            tableData = [['apples', 'oranges', 'cherries', 'bananas', 'kiwis'],
                         ['Alice', 'Bob', 'Carol', 'David'],
                         ['dogs', 'cats', 'moose', 'goose'],
                         ['rocks']]
                 
            def printTable(data):
                rows = len(max(data, key=len)) # get the number of rows based on longest array (if variable)
                cols = len(data) # get the number of columns
                colWidths = [0] * cols # set column widths to zero
                for c in range(cols):
                    colWidths[c] = len(max(data[c]))+2 # get length of longest word in each column and assign width to columns (added a little extra)
                    diff = rows - len(data[c]) # Check for rows with incomplete data (fewer entries than the longest row)
                    if diff < rows:
                        for x in range(diff):
                            data[c].append('') # Add blank strings to create rows of equal length
                for j in range(rows):
                    for i in range(cols):
                        cw = colWidths[i]
                        print(data[i][j].rjust(cw), end='')
                    print() # New row
            printTable(tableData)    
            

            【讨论】:

              【解决方案7】:
              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()
              

              【讨论】:

              • 感谢您的回答。对您的贡献的任何解释将不胜感激。你添加了什么,它是如何工作的等等。这将有助于用户了解如何从你的代码开始,并使其适应另一种情况。
              猜你喜欢
              • 2016-04-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2019-10-09
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2020-05-18
              相关资源
              最近更新 更多