printing with variable length of list

L

les ander

Hi,
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

however, the above line does not work--python says that float
argument is required.
So i did
print "%-10s "* len(d) % map(str,d)
But now I get:
TypeError: not enough arguments for format string

please help
thanks
 
R

Riccardo Galli

Hi,
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

however, the above line does not work--python says that float
argument is required.

You need a tuple, not a list
print "%-10f "* len(d) % tuple(d)

Riccardo

--
-=Riccardo Galli=-

_,e.
s~ ``
~@. ideralis Programs
.. ol
`**~ http://www.sideralis.net
 
A

Alex Martelli

les ander said:
Hi,
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

tuple(d) is one possibility, as Riccardo pointed out. A small loop
might also be nice, and conceptually simple:

for item in d:
print '%-10f'%item,
print

the comma at the end stops print from emitting a newline, and a space is
used between item; the print at the end ends the line.


Alex
 
D

Dan Bishop

Hi,
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

Try:

print "%-10f " * len(d) % tuple(d)
 
P

Peter Otten

les said:
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
....1.200000 31.100000 1.001000

which can be simplified to
1.200000 31.100000 1.001000

if you like. I'm assuming that the trailing space in your approach was
accidental.

Peter
 

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
474,209
Messages
2,571,088
Members
47,686
Latest member
scamivo

Latest Threads

Top