Catury
Catury

Reputation: 71

How to print a 2D array in a "pretty" format?

Hello so let's say I have a 2D array a = [[1,2,1,2], [3,4,5,3], [8,9,4,3]] and I would like to print this out in a grid like table. So far the code I have is:

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)

When this is printed out it gives me:

  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

And then the error:

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

IndexError: list index out of range

Can someone tell me why this is happening? I don't want the 'None' at the end of each row either. I want it to output:

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

Upvotes: 5

Views: 12569

Answers (4)

yassin
yassin

Reputation: 652

You got the first range wrong (and the second one should take the length of the current row) and you don't need a variable 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()

Upvotes: 0

Ziyad Edher
Ziyad Edher

Reputation: 2150

There are three main issues with the code you have posted: the way of iterating through the array, the assignment of the b variable to a the return of a print statement, and the printing of that b variable.

Firstly, the way you are iterating through the array is fairly counter-intuitive. You can simply use

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

to make it much more clear.

Secondly, your understanding of the print statement seems to be a bit lacking. The print statement takes in an argument and prints it out directly, so there is no need to assign it to a variable. Since the print statement has no official return, it automatically returns None, which ties in with the next point to explain the Nones at the end of your print statements.

Finally, the printing of the b variable which has a value None assigned to it as discussed above produces the Nones you see.


To fix your code, you could use the following solution.

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)

Other than the things stated above, this code differs by adding a print(""), which is equivalent to a new line, after every row in the array.

Upvotes: 1

Maaaaa
Maaaaa

Reputation: 388

You are using len(a[0]) in both loops.

use

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

Upvotes: 0

Xavier C.
Xavier C.

Reputation: 1971

Here is what your are using:

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)

You are using two for loops with len(a[0]) but your input data isn't a square, so that can't work!

You might consider using this:

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

That will give you this:

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 

Upvotes: 2

Related Questions