I have a list of integers that I need to print out as a matrix. I did that as following:
matrix = [mylist[i:i + n] for i in range(0, len(mylist), n)]
for nlist in matrix:
print(nlist)
This works well; for example if my list is [1 2 3 4 5 6 7 8 9] and n is 3, it will output:
[1 2 3]
[4 5 6]
[7 8 9]
Now I don't just want those integers, I want a zero in front of them; so 01 02 03 etc. I read that this is easily done with print("{:0=2d}".format(integer))
, but it doesn't seem to work on my list or matrix. print("{:0=2d}".format(mylist))
or print("{:0=2d}".format(nlist))
gives me an error message.
How can I make it work for a list?
Also, a very small thing I hope I'm allowed to ask here: how do I get rid of the square brackets around my matrix?
EDIT: I figured out why my code didn't work. I had this if statement that said that if the program encountered a 0, that zero had to be printed as a string "X". So sometimes I would have a list of both integers and a string. Like so:
08 07 06
05 04 03
02 01 X
Should I just convert everything to a string then? Or how can I make it work?