simplify printing of a list

B

beliavsky

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.
 
E

Eru

(e-mail address removed) 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 :)
 
L

Larry Bates

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.
 
P

Peter Otten

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
'[1, 2, 3]'

instead of
'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:
.... def __mul__(self, other):
.... return " ".join(map(self.__mod__, other))
....
Format("%6d") % 1 ' 1'
Format("%6d") * [1, 2, 3] ' 1 2 3'

Peter
 
J

Jeff Epler

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)

iD8DBQFAwGYgJd01MZaTXX0RAna7AKCNCDwhmu6BQKca2vB1thu8x9dQYgCfVMbJ
FIshxg3PLUA9HV/OTi0Cbko=
=PV6m
-----END PGP SIGNATURE-----
 
J

Josiah Carlson

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top