【问题标题】:How to print a 2D array in a "pretty" format?如何以“漂亮”格式打印二维数组?
【发布时间】:2016-11-05 17:51:53
【问题描述】:

您好,假设我有一个 2D 数组 a = [[1,2,1,2], [3,4,5,3], [8,9,4,3]],我想将其打印在表格等网格中。到目前为止我的代码是:

def printArray(a):
    for row in range(len(a[0])):
        for col in range (len(a[0])):
            b = print("{:8.3f}".format(a[row][col]), end = " ")
        print(b)

当它被打印出来时,它给了我:

  1.000    2.000    1.000    2.000 None
  3.000    4.000    5.000    3.000 None
  8.000    9.000    4.000    3.000 None

然后报错:

File "hw8pr2.py", line 17, in printArray
b = print("{:8.3f}".format(a[row][col]), end = " ")

IndexError: list index out of range

谁能告诉我为什么会这样?我也不希望每行末尾都有“无”。我希望它输出:

  1.000    2.000    1.000    2.000
  3.000    4.000    5.000    3.000
  8.000    9.000    4.000    3.000

【问题讨论】:

  • 你不需要声明一个变量来打印东西。它没有返回,因为一旦带有变量 b 的 for 循环结束,变量 b 实际上什么都没有。删除最后一行。
  • 您对列和行都使用len(a[0])。如果您有一个 sqare 矩阵,它会起作用,但对于您给出的示例,情况并非如此。
  • 为了打印漂亮的表格,Python 有一个巧妙地命名为 prettytable 的模块。

标签: python arrays list 2d


【解决方案1】:

这是你正在使用的:

def printArray(a):
    for row in range(len(a[0])):
        for col in range (len(a[0])):
            b = print("{:8.3f}".format(a[row][col]), end = " ")
        print(b)

您正在使用两个带有 len(a[0]) 的 for 循环,但您的输入数据不是正方形,因此无法正常工作!

你可以考虑使用这个:

def printA(a):
    for row in a:
        for col in row:
            print("{:8.3f}".format(col), end=" ")
        print("")

这会给你这个:

In [14]: a = [[1, 2, 1, 2], [3, 4, 5, 3], [8, 9, 4, 3]]

In [15]: printA(a)
   1.000    2.000    1.000    2.000 
   3.000    4.000    5.000    3.000 
   8.000    9.000    4.000    3.000 

In [16]: b = [[1, 2, 1, 2, 5], [3, 4, 7, 5, 3], [8, 2, 9, 4, 3], [2, 8, 4, 7, 6]]

In [17]: printA(b)
   1.000    2.000    1.000    2.000    5.000 
   3.000    4.000    7.000    5.000    3.000 
   8.000    2.000    9.000    4.000    3.000 
   2.000    8.000    4.000    7.000    6.000 

【讨论】:

    【解决方案2】:

    您发布的代码存在三个主要问题:遍历数组的方式、将b 变量分配给print 语句的返回以及该b 的打印变量。

    首先,您遍历数组的方式是相当违反直觉的。你可以简单地使用

    def printArray(arr):
        for row in arr:
            for item in row:
                # code for printing
    

    让它更清楚。

    其次,你对print语句的理解似乎有点欠缺。 print 语句接受一个参数并直接将其打印出来,因此无需将其分配给变量。由于 print 语句没有正式返回,它会自动返回 None,这与下一点解释 print 语句末尾的 Nones 相关。

    最后,b 变量的打印,如上面所讨论的,其分配了一个值 None,产生了您所看到的 Nones。


    要修复您的代码,您可以使用以下解决方案。

    a = [[1,2,1,2], [3,4,5,3], [8,9,4,3]]
    
    def printArray(arr):
        for row in arr:
            for item in row:
                print("{:8.3f}".format(item), end = " ")
            print("")
    
    printArray(a)
    

    除上述内容外,此代码的不同之处在于在数组中的每一行之后添加了一个print(""),这相当于一个新行。

    【讨论】:

      【解决方案3】:

      您在两个循环中都使用了 len(a[0])。

      使用

      for row in range(len(a)):
        for col in range(len(a[0])):
      

      【讨论】:

        【解决方案4】:

        你弄错了第一个范围(第二个应该取当前行的长度),你不需要变量 b:

        def printArray(a):
            for row in range(len(a)):
                for col in range (len(a[row])):
                    print("{:8.3f}".format(a[row][col]), end = " ")
                print()
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-06-24
          • 2012-10-24
          • 2014-03-31
          • 2011-03-21
          • 2012-10-01
          • 1970-01-01
          • 2011-05-06
          相关资源
          最近更新 更多