Jump to content

How to create an integer 2D array in Python and print it in a single line


zak100

Recommended Posts

Hi,

I am trying to create an integer 2D array. I have used numpy, but it creates a floating point array. When I am printing this 8 * 8 array, its printing  each entry of 8*8 array in a separate line. I want to print each row on a new line

import numpy as np

class Board:
    Matrix = np.empty((8, 8))
    Matrix.fill(0)
    def __init__(self, n):
        self.n = n

    def displayBDMatrix(self, n):
        for i in range(n):
            for j in range(n):
                print(Board.Matrix[i][j]),

    def main(self):
        n=8
        #self.display(n)
        self.displayBDMatrix(n)
        #self.displayPieces()

if __name__ == "__main__":
    objBoard = Board(8)
    objBoard.main()

I am getting following output:

0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
and so on.

Somebody please guide me.

Zulfi.

 

Edited by zak100
Link to comment
Share on other sites

2 hours ago, zak100 said:

Somebody please guide me.

Some general methods that can be used to approach the issue:

(A)

  1. Get rid of the large example
  2. Focus on something simple and generic that also behaves in the same unwanted way.
  3. Figure out why the simple example fails
  4. Apply fix to main example

or

(B)

  1. Run the example in a debugger
  2. Examine the output for the line(s) that produces the result you consider incorrect
  3. Update the code and retry

or

(C)

  1. Study the functions or APIs that you want to use
  2. Use the function according to the documentation

or

(D)

  1. Start from the required behaviour (print a matrix)
  2. Locate libraries or functions that according to documentation displays the required properties

Note that the methods above can be applied in a wide set of programming issues, not just 8*8 arrays.  There may also be other methods, like posting on a python or programming forum. 

(A) A simple example:

print(a)
print(b)

result, print function produces a newline:

1                     
2

Modify simple example

print(1, end=",")
print(2)

result:

1,2

(B) Debuggers have been covered in other threads opened by you. 

 

(C) Documentation of the built in print function

The documentation of print available at for instance https://docs.python.org/3/library/functions.html#print will tell you that print by default ends the output with a newline character loch you may not want. 

 

(D) Printing a matrix 

Since numpy is used it may be convenient to use the available methods in numpy:

print(np.matrix(Board.Matrix))

 

Please follow up on which method(s) you used to solve the issue.

 

Link to comment
Share on other sites

Hi,

Thanks a lot.

I have to learn debugger. Yes people suggested me to use the debugger, I tried but then got problems and stopped. I would specifically start a thread for debugger. Thanks for your solution using numpy. I solved the problem last night. My solution is more traditional

import numpy as np
#from array import *

class Board:
    Matrix = np.array([[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]])
    #Matrix.fill(0)
    def __init__(self, n):
        self.n = n

    def displayBDMatrix(self, n):
        #for i in range(n):
            #for j in range(n):
                #print(Board.Matrix[i][j]),
        for i in range(n):
            print(str(Board.Matrix[i][0]) + " " + str(Board.Matrix[i][1]) + " " + str(Board.Matrix[i][2]) + " " + str(Board.Matrix[i][
                3]) + " " + str(Board.Matrix[i][4]) + " " + str(Board.Matrix[i][5]) + " " + str(Board.Matrix[i][6]) + " " + str(Board.Matrix[i][
                      7]))

    def main(self):
        n=8
        #self.display(n)
        self.displayBDMatrix(n)
        #self.displayPieces()

if __name__ == "__main__":
    objBoard = Board(8)
    objBoard.main()

 

Quote

0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0

 

Thanks a lot. God blesses you.

Zulfi.

Link to comment
Share on other sites

 

3 hours ago, zak100 said:

Thanks for your solution using numpy.

Thanks for the feedback.

 

3 hours ago, zak100 said:

I solved the problem last night. My solution is more traditional

Just in case some other member or visitor happens to find this topic: frankly, I would never consider using your solution. 

As a comparison, here is an alternative version of creating an 8x8 zero filled array and printing it. 

import numpy as np
a = np.zeros([8, 8], dtype = int) 
for t in a: print(*t)

 

Note: Im not going to go through all issues, but your code (still) produces identical output regardless of the parameter value; calling Board(1) or Board(8) does not matter, it creates and prints an 8x8 matrix. 

 

Edited by Ghideon
example added
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.