0

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?

Rose
  • 109
  • 4

5 Answers5

2

The only way I can think of to add a zero is to specify them all as strings. Then also to get rid of the brackets you will have to print each number separately.

for nlist in matrix:
    for value in nlist:
        print("0" + str(value), end=" ")
    print("")
13ros27
  • 184
  • 1
  • 16
0
>>> l = [1,2,3] 
>>> " ".join(list(map(lambda x : "{:0=2d}".format(x), l)))
'01 02 03' 

with no brackets, so try...

matrix = [mylist[i:i + n] for i in range(0, len(mylist), n)]
for nlist in matrix:
    print(" ".join(list(map(lambda x : "{:0=2d}".format(x), nlist))))

edit

With the mixed lists you can change the lambda a little like this,

>>> matrix = [[1,2,3],[4,'x',6],[7,8,9]]
>>> for nlist in matrix:
...     print(" ".join(list(map(lambda x: "{:0=2d}".format(x) if isinstance(x,int) else x, nlist))))
... 
01 02 03
04 x 06
07 08 09
corn3lius
  • 4,857
  • 2
  • 31
  • 36
  • You really don't need the lambda ;-) – Paul Panzer Dec 01 '17 at 17:48
  • Thanks for your help! I'm getting a `ValueError: Unknown format code 'd' for object of type 'str'` error. Do you know what could cause this? – Rose Dec 01 '17 at 17:51
  • @Rose you're passing in strings not int in your list? – corn3lius Dec 01 '17 at 17:52
  • They are supposed to be integers yes, but I probably messed up somewhere and made them strings haha. I'll look into it. – Rose Dec 01 '17 at 17:55
  • @corn3lius Let me be more specific: `"{:0=2d}".format` is a perfectly good function object which you can pass directly to `map`. Try `" ".join(list(map("{:0=2d}".format, l)))`. – Paul Panzer Dec 01 '17 at 19:07
0

EDIT: Supporting new condition that 0's should print as X and wrapping parens around print for python 3.x.

Suppose your integers are stored in a list l.

l = [1, 2, 3, 4, 5, 6, 7, 8, 0]  
n = 3

for i in range(n):
    print(" ".join(["{:0=2d}".format(x) if x != 0 else ' X' for x in l[n*i:n*(i+1)]]))

Output:

01 02 03
04 05 06
07 08  X

The explanation is that we loop through the list l in groups of n elements at a time (l[n*i:n*(i+1)]) (let's call this a sublist). Then, we format each element of the sublist, we apply the format function to convert it to a string as you want. Finally join the formatted sublist together with spaces and print.

pault
  • 41,343
  • 15
  • 107
  • 149
0
n = 3
mylist = [1, 2, 3, 4, 5, 6, 7, 8, 9]
matrix = [mylist[i:i + n] for i in range(0, len(mylist), n)]
for nlist in matrix:
    mstr = ''
    for i in nlist:
        mstr += "{:0=2d} ".format(i)
    print(mstr)

Will give you

01 02 03 
04 05 06 
07 08 09 
Ian E
  • 142
  • 2
  • 12
0

Your matrix is a list of lists. So when you are doing

for nlist in matrix:
        print(nlist)

The nlist would be list and you are printing it directly without unpacking. So if you want to avoid the brackets, you can just do print(*nlist). For more on that, refer to unpacking.

If you want to add 0's in front of every element, you can utilize the parameters of print function like sep and end. This will work whether the element is int or str

matrix = [mylist[i:i + n] for i in range(0, len(mylist), n)]
for nlist in matrix:
    for item in nlist:
      print('0',item,sep='',end=' ')
    print()
yash
  • 1,357
  • 2
  • 23
  • 34