473,394 Members | 1,878 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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!
Mar 17 '08 #1
3 14608
elcron
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
2,851 Expert Mod 2GB
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
Being a new user of Python I never considered joining a for loop. Thanks for your help.
Mar 18 '08 #4

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

Similar topics

4
by: Jody Gelowitz | last post by:
I am having a problem with printing selected pages. Actually, the problem isn't with printing selected pages as it is more to do with having blank pages print for those pages that have not been...
7
by: Dennis C. Drumm | last post by:
I was wondering if someone could suggest were/how I could get started with setting up printing of a collection of text that includes tables from a grid and other supporting information about the...
10
by: Nevets Steprock | last post by:
I'm writing a web program where one of the sections is supposed to output a correlation matrix. The typical correlation matrix looks like this: ..23 ..34 .54 ..76 .44 .28 ..02 .77 ...
1
by: mark | last post by:
I can't seem to get the logic for printing multiple pages. I know I have to do a comparison between the bottom margin and the position of the next line to be printed and initiate a has more pages...
3
by: D Witherspoon | last post by:
No matter what I do the default paper size is always either A3 or 11 by 8.5 .. Here is the code. Dim dlg As DialogResult pd.DocumentName = "Weld Image" Dim pkPaperSize As New...
1
by: Paul H | last post by:
I am developing a database where some reports need to print to a laser and some need to print to a dot matrix printer. I am not interested in how to do this programmatically, I will let the user...
2
by: Sukh | last post by:
Hi I am stuck with a problem Can anyone help me out from this... I am printing a report on pre-printed continue paper using dot-matrix printer using vb.net. Data is printing on all the...
9
by: Dadio | last post by:
Hi! I have to take some strings from a file and put them in a record... The various strings in the file are written on this way: string1|string2|string3|string4|string5| This is the program...
1
by: amcgary | last post by:
Hello, I am trying to print a System.Drawing.Printing.PrintDocument to a dot matrix printer using C# .NET. I have created an instance of the PrintDocument and create a event handler for the...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.