443,818 Members | 1,262 Online 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
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 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! 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 if True:     lol = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]       print "Columns %s"%( " ".join([str(j) for j in range(len(lol)]) )     for i in range(0,len(lol)):         print " Row %s: %s"%(i, " ".join( [str(j) for j in lol[i]]))   Mar 18 '08 #2

 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 def columnize(word, width, align='Left'):     nSpaces = width - len(word)     if nSpaces < 0:         nSpaces = 0     if align == 'Left':         return word + (" " * nSpaces)     if align == 'Right':         return (" " * nSpaces) + word     return (" " * (nSpaces/2)) + word + (" " * (nSpaces-nSpaces/2))   num = int(raw_input('Gimme a starting int: ')) n = int(raw_input('Matrix size: '))   listofLists = [[i+j for i in range(num,num+n)] for j in range(0,n*n,n)]   column = 7   print print '%s%s' % (columnize('Row/Column |', column*2, 'Right'), \                 '|'.join([columnize('Col %d' % i, column, 'Center') \                           for i in range(len(listofLists))])) spaces = sum([column+1 for i in range(len(listofLists))])+column*2 print '='*spaces for i, item in enumerate(listofLists):     print '%s%s' % (columnize('Row %d |' % i, column*2, 'Right'), \                     '|'.join([columnize(str(num), column, 'Center') \                               for num in item])) Output: Expand|Select|Wrap|Line Numbers >>>    Row/Column | Col 0 | Col 1 | Col 2  ======================================        Row 0 |   1   |   2   |   3           Row 1 |   4   |   5   |   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 