I'd like to make a code to obtain the following result.
Input: mylist=[[-1,3,111,'b'],[0,'a',1],[-2,1]]
Output: -1 111 'b'
3 'a' 1
-2 1
I tried to make the code as follows.
blank1=' '
for x in mylist:
for y in x:
print blank1 if y==0 else y,
print
print
But the result is not what I want. I'd like to make each entry align to center Where should I fix it?
Use str.center
for x in mylist:
s=""
for y in x:
s+=str(" " if y==0 else y).center(10)
print s
Change 10 to whatever width you want.
If you really want those qoutes you can use this:
s+=str(" " if y==0 else "'"+y+"'" if isinstance(y, basestring) else y).center(10)
Output of this code with the quote twitch using your array:
-1 111 'b'
'a' 1
-2 1