zak100 Posted September 27, 2020 Posted September 27, 2020 (edited) 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 September 27, 2020 by zak100
Ghideon Posted September 27, 2020 Posted September 27, 2020 2 hours ago, zak100 said: Somebody please guide me. Some general methods that can be used to approach the issue: (A) Get rid of the large example Focus on something simple and generic that also behaves in the same unwanted way. Figure out why the simple example fails Apply fix to main example or (B) Run the example in a debugger Examine the output for the line(s) that produces the result you consider incorrect Update the code and retry or (C) Study the functions or APIs that you want to use Use the function according to the documentation or (D) Start from the required behaviour (print a matrix) 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. 1
zak100 Posted September 27, 2020 Author Posted September 27, 2020 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.
Ghideon Posted September 27, 2020 Posted September 27, 2020 (edited) 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 September 27, 2020 by Ghideon example added 1
zak100 Posted September 30, 2020 Author Posted September 30, 2020 Hi, Thanks a lot. That's why posted this problem because I new that somebody will tell me the good approach. I did not this solution on the internet. So we have got something new. God blesses you . Zulfi.
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now