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. 5 1859 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"}
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. 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
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-----
> 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 This thread has been closed and replies have been disabled. Please start a new discussion. 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: 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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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)...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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...
| |