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!
3 14625
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!
You can access nested lists the same you could try something like li[i][j] but the following is more scalable -
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]]))
-
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. -
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[0]))]))
-
spaces = sum([column+1 for i in range(len(listofLists[0]))])+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: -
>>>
-
Row/Column | Col 0 | Col 1 | Col 2
-
======================================
-
Row 0 | 1 | 2 | 3
-
Row 1 | 4 | 5 | 6
-
Row 2 | 7 | 8 | 9
Being a new user of Python I never considered joining a for loop. Thanks for your help.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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 =...
|
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,...
|
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:
|
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...
|
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)
| |
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...
|
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
|
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>
|
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...
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |