Issue
So I have a grid I want to print which is:
['0', '0', '0', '0']
['0', '0', '0', '0']
['0', '0', '0', '0']
['0', '0', '0', '0']
I want it to be:
[0 0 0 0]
[0 0 0 0]
[0 0 0 0]
[0 0 0 0]
i.e. remove ONLY quotes '
& comma ,
.
The code I tried:
vertical=[]
horizontal=[]
size=5
def printgrid():
for b in range(size):
print(*vertical[b] ,sep='')
for j in range(size):
for i in range(size):
horizontal.append(0)
vertical.append(horizontal)
horizontal=[]
printgrid()
My code prints:
0000
0000
0000
0000
I tried using a few tutorials but it prints either without quotes only or without anything at all.
Solution
try:
vertical=[]
horizontal=[]
size=5
def printgrid():
for b in range(size):
print("[" + " ".join([str(i) for i in vertical[b]])+ "]")
for j in range(size):
for i in range(size):
horizontal.append(0)
vertical.append(horizontal)
horizontal=[]
printgrid()
" ".join(x)
joins all the elements in x
together with separator " "
[str(i) for i in vertical[b]]
creates a new list where all the elements are the string equivalents of the original list
Answered By - tomdartmoor
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.