printing with variable length of list
Peter Otten
__peter__ at web.de
Tue Oct 19 02:59:05 EDT 2004
les ander wrote:
> suppose I have a list
> d=[1.2 , 31.1, 1.001]
> in general i will not know the size of d
> however, I would like to print them out nicely in the row format
> such as:
> print "%-10f "* len(d) % d
Here is a functional variant:
>>> d = [1.2 , 31.1, 1.001]
>>> def format(f):
... return "%-10f" % f
...
>>> print " ".join(map(format, d))
1.200000 31.100000 1.001000
which can be simplified to
>>> print " ".join(map("%-10f".__mod__, d))
1.200000 31.100000 1.001000
if you like. I'm assuming that the trailing space in your approach was
accidental.
Peter
More information about the Python-list
mailing list