473,788 Members | 2,800 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

simplify printing of a list

To print a list with a specified format one can write (for example)

for j in [0,1,2]:
print "%6d"%j,
print

The code

print "%6d"%[0,1,2]

currently produces a syntax error, but it would be convenient if it
had the same meaning as the loop above.

One can write a function to print a list, for example

def print_list(x,fm t_x="%6d"):
""" print a list on one line """
for y in x: print fmt_x % y,

print_list([0,1,2])

but it gets messy to print several lists on the same line.

In Fortran 90/95 one can write

print "(100i6)",(/0,1,2/)

where the format (100i6) means that UP TO 100 integers are printed
using 6 columns. An alternative suggestion I have for Python is to
allow

print "100%6d"%[0,1,2]

with the same meaning.

I realize that what I am asking for is just a convenience, but it is
one that I could use in almost every program I write.
Jul 18 '05 #1
5 1880
Eru
be*******@aol.c om escribio:
To print a list with a specified format one can write (for example)

for j in [0,1,2]:
print "%6d"%j,
print

The code

print "%6d"%[0,1,2]

currently produces a syntax error, but it would be convenient if it
had the same meaning as the loop above.

One can write a function to print a list, for example

def print_list(x,fm t_x="%6d"):
""" print a list on one line """
for y in x: print fmt_x % y,

print_list([0,1,2])
How about using:

def fmtlst(fmt,lst) :
return fmt*len(lst) % tuple(lst)

print fmtlst("%3d",ra nge(5)), fmtlst("%5.2f", [1.2, 3, 4.567])

This prints:

0 1 2 3 4 1.20 3.00 4.57

Maybe it gets your job done (though it's not the most efficient thing
one can do...)

but it gets messy to print several lists on the same line.

In Fortran 90/95 one can write

print "(100i6)",(/0,1,2/)

where the format (100i6) means that UP TO 100 integers are printed
using 6 columns. An alternative suggestion I have for Python is to
allow

print "100%6d"%[0,1,2]
It looks a bit weird (unpythonic) to me, and we can use workarounds :)

with the same meaning.

I realize that what I am asking for is just a convenience, but it is
one that I could use in almost every program I write.


--
Daniel Ripolles ( Eru )
Make Source, Not War
for(0..pop){for ($c=$_%2;$_>>=1 ;){$c=$_%2 .$c}print"$c\n" }

Jul 18 '05 #2
How about:

print ''.join(["%6d" % j for j in [0,1,2]])

or

print reduce(lambda x,y: x+"%6d" % y, [0,1,2], '')

or

t=[sys.stdout.writ e("%6d" % j for j in [0,1,2]]
(note: the t assignment is so the list comprehension
results don't print)

all work and are pythonic in nature. I didn't
test, but I'll be the last one is the most
efficient.

HTH,
Larry Bates
Syscon, Inc.

<be*******@aol. com> wrote in message
news:30******** *************** ***@posting.goo gle.com...
To print a list with a specified format one can write (for example)

for j in [0,1,2]:
print "%6d"%j,
print

The code

print "%6d"%[0,1,2]

currently produces a syntax error, but it would be convenient if it
had the same meaning as the loop above.

One can write a function to print a list, for example

def print_list(x,fm t_x="%6d"):
""" print a list on one line """
for y in x: print fmt_x % y,

print_list([0,1,2])

but it gets messy to print several lists on the same line.

In Fortran 90/95 one can write

print "(100i6)",(/0,1,2/)

where the format (100i6) means that UP TO 100 integers are printed
using 6 columns. An alternative suggestion I have for Python is to
allow

print "100%6d"%[0,1,2]

with the same meaning.

I realize that what I am asking for is just a convenience, but it is
one that I could use in almost every program I write.

Jul 18 '05 #3
be*******@aol.c om wrote:
To print a list with a specified format one can write (for example)

for j in [0,1,2]:
print "%6d"%j,
print

The code

print "%6d"%[0,1,2]

currently produces a syntax error, but it would be convenient if it
had the same meaning as the loop above.
A TypeError. What if I wanted the current behaviour, e. g
"%s" % [1, 2, 3] '[1, 2, 3]'

instead of
"%s" % [1, 2, 3] '1 2 3' #faked

One can write a function to print a list, for example

def print_list(x,fm t_x="%6d"):
""" print a list on one line """
for y in x: print fmt_x % y,

print_list([0,1,2])

but it gets messy to print several lists on the same line.


How about different operators for the two formatting operations:
class Format(str): .... def __mul__(self, other):
.... return " ".join(map(self .__mod__, other))
....
Format("%6d") % 1 ' 1' Format("%6d") * [1, 2, 3] ' 1 2 3'


Peter

Jul 18 '05 #4
On Thu, Jun 03, 2004 at 03:08:57PM -0700, be*******@aol.c om wrote:
To print a list with a specified format one can write (for example)

for j in [0,1,2]:
print "%6d"%j,
print

The code

print "%6d"%[0,1,2]

currently produces a syntax error,
No, it doesn't. (it produces a TypeError)
but it would be convenient if it
had the same meaning as the loop above.


No, it wouldn't.

[remainder deleted]

Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFAwGYgJd0 1MZaTXX0RAna7AK CNCDwhmu6BQKca2 vB1thu8x9dQYgCf VMbJ
FIshxg3PLUA9HV/OTi0Cbko=
=PV6m
-----END PGP SIGNATURE-----

Jul 18 '05 #5
> print "100%6d"%[0,1,2]

The real problem is that "100%6d" already has a meaning in string
formatting, though not what you like. On the most part, Python string
formatting conventions follow C string formatting conventions. Python
has added "%(key)s" formatting, and there may be $-formatting in the
future (I stopped following that thread months ago).

Again, the syntax you offer "100%6d" is already valid, if meaning
something else.

- Josiah
Jul 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
4858
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 = Blank Page 3 = Print OK Page 4 = Blank
1
2090
by: NickB | last post by:
Please could someone tell me what is wrong. Ther error is: An unhandled exception of type 'System.NullReferenceException' occurred in microsoft.visualbasic.dll Additional information: Object variable or With block variable not set. I am trying to print the contents of a list box on another form. This is the sub, and the highlighted line is where it is falling over.
0
1296
by: ALMA_J_III | last post by:
Hi all! Customer will use HP LaserJet 4200TN printer for printing of invoices. 3 different versions of invoice on different color of paper should be to print. HP LaserJet 4200TN have 3 paper source trays. Maybe some from you have experience in printing to different trays of this printer. Crystalreport Report object has PrintOptions class and the next papersource properties:...
6
482
by: Bill | last post by:
Hi I am trying to get my listbox items to print if they stream past the one page mark. my code is working for one page of information (if the e.hasmorepages) is not there. But I am having trouble getting it to print on multiple pages. The second page will not print at all. Thanks in advance
0
1694
by: Hank | last post by:
We have been printing pdf files through Adobe, from Access for several years. Adobe version 5.0 is currently installed. Recently we have received PDF files that were created under the Adobe 7.0 version. When trying to view a file manually Adobe 5.0 displays a pop up message: "This file may contain newer information than this viewer can support. It may not open or display correctly". There is a check box with the pop-up that says,...
2
9977
by: Brad Pears | last post by:
I have a vb.net 2005 application and am using the print preview screen. This screen has a printer icon on it that the user can use to print the document currently being viewed. It uses the default printer settings to print. I wanted the print preview to appear the same for all users (i.e. a default page size of 8.5x14 (legal) and portrait mode). Many users have different printers as their default (plotters etc..) and I found that various...
4
16824
ADezii
by: ADezii | last post by:
Recently, there seems to be several questions specifically related to Printers and changing Printing characteristics for Forms and Reports. For this reason alone, I decided to dedicate this week's Tip to these Topics. The Tip will actually consist of several Tips which I feel are very useful for all Users, from Newbies to Experts. In order to utilize the code contained within these Tips, you must have Access 2002 or later. How can I...
3
4146
by: tshad | last post by:
I have dataGrid that I am filling from a List Collection and need to sort it by the various columns. So I need to be able to sort the Collection and found that you have to set up your own sorting functions to make it work. I have build the following 3 sorting functions for the 3 columns and have to call each one specifically. I am trying to find a way to cut simplify the functions and calls to make this a more
5
3676
by: Jon Skeet [C# MVP] | last post by:
On Sep 9, 9:41 am, raylopez99 <raylope...@yahoo.comwrote: It's tricky in .NET for two reasons: 1) LINQ doesn't have any concept of "remove" 2) List<T>.RemoveAll doesn't pass in the index (which makes sense as it would then need to If List<Tsupported some sort of "view" which also exposed RemoveAll, it would be easy:
0
9656
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9498
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10172
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10110
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9967
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7517
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6750
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5536
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4069
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.