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 14608
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
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. -
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...
|
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...
|
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 ...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
| |