473,568 Members | 2,850 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help with Printing Matrix Grid

4 New Member
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 14625
elcron
43 New Member
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 Recognized Expert Moderator Specialist
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
okcomputer24
4 New Member
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
4839
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 selected. For example, if I were to have 5 pages with every second page printing, I would get the following results: Page 1 = Print OK Page 2 =...
7
2478
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 grid's contents. If you could just point me at the classes that I should consider and some very broad guidelines that would be great. For instances,...
10
1820
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 .80 .99 I've written code to calculate the correlation data and it is populated in a vector like this:
1
4806
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 condition when they are equal. I have included my code for the printpage handler below. When I run it for an array that takes up less than a page its...
3
31830
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 Printing.PaperSize("sdfgsfdg", 850, 1100)
1
2856
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 manually select the printer for each report, my question is..... Is printing to a dot matrix printer the same (as far as Access in concerned) as...
2
2530
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 locations. But After printing first page it increase paper 3cm vertically/Height so on second
9
2039
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 that i have just made...what's wrong? #include<stdio.h> #include<conio.h> #include<string.h>
1
5381
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 PrintPage property of the PrintDocument. PrintDocument document = new PrintDocument(); document.PrintPage += new...
0
7693
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7604
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7962
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6275
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5498
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3631
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1207
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
932
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.