Help with Printing Matrix Grid

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • okcomputer24
    New Member
    • Mar 2008
    • 4

    Help with Printing Matrix Grid

    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!
  • elcron
    New Member
    • Sep 2007
    • 43

    #2
    Originally posted by okcomputer24
    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:
    [code=python]
    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]
    [/code]
    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
    [code=python]
    if True:
    lol = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

    print "Columns %s"%( " ".join([str(j) for j in range(len(lol[0])]) )
    for i in range(0,len(lol )):
    print " Row %s: %s"%(i, " ".join( [str(j) for j in lol[i]]))
    [/code]

    Comment

    • bvdet
      Recognized Expert Specialist
      • Oct 2006
      • 2851

      #3
      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.[code=Python]
      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(listo fLists[0]))]))
      spaces = sum([column+1 for i in range(len(listo fLists[0]))])+column*2
      print '='*spaces
      for i, item in enumerate(listo fLists):
      print '%s%s' % (columnize('Row %d |' % i, column*2, 'Right'), \
      '|'.join([columnize(str(n um), column, 'Center') \
      for num in item]))[/code]

      Output:
      [code=Python]
      >>>
      Row/Column | Col 0 | Col 1 | Col 2
      =============== =============== ========
      Row 0 | 1 | 2 | 3
      Row 1 | 4 | 5 | 6
      Row 2 | 7 | 8 | 9 [/code]

      Comment

      • okcomputer24
        New Member
        • Mar 2008
        • 4

        #4
        Being a new user of Python I never considered joining a for loop. Thanks for your help.

        Comment

        Working...