473,320 Members | 1,969 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,320 software developers and data experts.

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,fmt_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 1859
Eru
be*******@aol.com 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,fmt_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",range(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.write("%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.google.c om...
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,fmt_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.com 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,fmt_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.com 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)

iD8DBQFAwGYgJd01MZaTXX0RAna7AKCNCDwhmu6BQKca2vB1th u8x9dQYgCfVMbJ
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
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...
1
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...
0
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...
6
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...
0
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...
2
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...
4
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...
3
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...
5
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.