i want to show a board that is for something like sudoku: the result have some bracket which i don't want them and I don't want the last line
board = [
[
[
[None, None, None], [None, None, None], [None, None, None],
],
[
[None, None, None], [None, None, None], [None, None, None],
],
[
[None, None, None], [None, None, None], [None, None, None],
]
],
[
[
[None, None, None], [None, None, None], [None, None, None],
],
[
[None, None, None], [None, None, None], [None, None, None],
],
[
[None, None, None], [None, None, None], [None, None, None],
]
],
[
[
[None, None, None], [None, None, None], [None, None, None],
],
[
[None, None, None], [None, None, None], [None, None, None],
],
[
[None, None, None], [None, None, None], [None, None, None],
]
],
]
for sq in board:
for row in sq:
print('|', end='')
for cell in row:
print(cell, end='|')
print()
print(' ', '------------------ '*3, sep='')
if someone help me I'd be pleasure
CodePudding user response:
The reason why you have brackets is because you are printing the list.
One way to unpack or print out all the values of that list as single values is to use a for loop:
>>> items = ["apple", "ball", "car"]
>>> for item in items:
>>> # Printing the values on the same line
>>> print(item, end="")
>>> apple ball car
Another thing you could do is to use a cool feature in Python to unpack the list in one line and we do this by using an astrisk (*):
>>> items = ["apple", "ball", "car"]
>>> print(*items)
>>> apple ball car
Here is the whole code including the fix for NOT having the last line:
# There are 3 squares and we dont want the line after the 3rd block.
# So we are using 'count' in order to keep track in which square
# we are in.
count = 1
for sq in board:
for row in sq:
print('|', end='')
for cell in row:
# By using a * we can unpack the list without having
# to use a for loop
print(*cell, end='|')
print()
# If we are in the 3rd square dont include a line
if count != 3:
print(' ', '-------------- '*3, sep='')
count =1
CodePudding user response:
This is because the variable cell
is a list and thats how lists are printed. To fix it youd have to loop through cell
OR
You can use the list unpacking operator *
. Make the following change
for cell in row:
# print(cell, end='|')
print(*cell, end='|')
Additionally, the default seperator between elements is " "
.
If you wish to change that to something else, use the sep
keyword in the print()
statement
CodePudding user response:
You can print it as a numpy array:
import numpy
print(numpy.array(board))
Output:
[[[[None None None]
[None None None]
[None None None]]
[[None None None]
[None None None]
[None None None]]
[[None None None]
[None None None]
[None None None]]]
[[[None None None]
[None None None]
[None None None]]
[[None None None]
[None None None]
[None None None]]
[[None None None]
[None None None]
[None None None]]]
[[[None None None]
[None None None]
[None None None]]
[[None None None]
[None None None]
[None None None]]
[[None None None]
[None None None]
[None None None]]]]