By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,818 Members | 1,262 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,818 IT Pros & Developers. It's quick & easy.

Help with Printing Matrix Grid

P: 4
I am wanting to print a matrix grid which is derived from a list of lists. The grid should look as follows:

Col 0 Col 1 Col 2

Row 0 1 2 3
Row 1 4 5 6
Row 2 7 8 9

However, I can't figure out how to print the lists without the [ ] around them. My code so far is as shown:

lol = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

for i in range(0,len(lol)):
print " "*5 +"Col", i,
print "\n"
for i in range(0,len(lol)):
print "Row",i,lol[i]

Any suggestions or advice would be greatly appreciated. Thanks!
Mar 17 '08 #1
Share this Question
Share on Google+
3 Replies


P: 43
I am wanting to print a matrix grid which is derived from a list of lists. The grid should look as follows:

Col 0 Col 1 Col 2

Row 0 1 2 3
Row 1 4 5 6
Row 2 7 8 9

However, I can't figure out how to print the lists without the [ ] around them. My code so far is as shown:
Expand|Select|Wrap|Line Numbers
  1. lol = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
  2.  
  3. for i in range(0,len(lol)):
  4.         print " "*5 +"Col", i,
  5. print "\n"
  6. for i in range(0,len(lol)):
  7.         print "Row",i,lol[i]
  8.  
Any suggestions or advice would be greatly appreciated. Thanks!
You can access nested lists the same you could try something like li[i][j] but the following is more scalable
Expand|Select|Wrap|Line Numbers
  1. if True:
  2.     lol = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
  3.  
  4.     print "Columns %s"%( " ".join([str(j) for j in range(len(lol[0])]) )
  5.     for i in range(0,len(lol)):
  6.         print " Row %s: %s"%(i, " ".join( [str(j) for j in lol[i]]))
  7.  
Mar 18 '08 #2

bvdet
Expert Mod 2.5K+
P: 2,851
The following uses a neat function that I picked up on this site from a tip by Bartonc. I haven't seen Bartonc in a while.
Expand|Select|Wrap|Line Numbers
  1. def columnize(word, width, align='Left'):
  2.     nSpaces = width - len(word)
  3.     if nSpaces < 0:
  4.         nSpaces = 0
  5.     if align == 'Left':
  6.         return word + (" " * nSpaces)
  7.     if align == 'Right':
  8.         return (" " * nSpaces) + word
  9.     return (" " * (nSpaces/2)) + word + (" " * (nSpaces-nSpaces/2))
  10.  
  11. num = int(raw_input('Gimme a starting int: '))
  12. n = int(raw_input('Matrix size: '))
  13.  
  14. listofLists = [[i+j for i in range(num,num+n)] for j in range(0,n*n,n)]
  15.  
  16. column = 7
  17.  
  18. print
  19. print '%s%s' % (columnize('Row/Column |', column*2, 'Right'), \
  20.                 '|'.join([columnize('Col %d' % i, column, 'Center') \
  21.                           for i in range(len(listofLists[0]))]))
  22. spaces = sum([column+1 for i in range(len(listofLists[0]))])+column*2
  23. print '='*spaces
  24. for i, item in enumerate(listofLists):
  25.     print '%s%s' % (columnize('Row %d |' % i, column*2, 'Right'), \
  26.                     '|'.join([columnize(str(num), column, 'Center') \
  27.                               for num in item]))
Output:
Expand|Select|Wrap|Line Numbers
  1. >>> 
  2.   Row/Column | Col 0 | Col 1 | Col 2 
  3. ======================================
  4.        Row 0 |   1   |   2   |   3   
  5.        Row 1 |   4   |   5   |   6   
  6.        Row 2 |   7   |   8   |   9   
Mar 18 '08 #3

P: 4
Being a new user of Python I never considered joining a for loop. Thanks for your help.
Mar 18 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.