user4261137
user4261137

Reputation:

Print several arrays via formatted print

I have two different arrays

A = [1, 2, 3]
B = [5, 6, 7] 

and I want to print these arrays in one line like:

1.00000 2.00000 3.00000 5.00000 6.00000 7.00000

How do I accomplish that with a * or ** operator? I always obtain a SyntaxError.

Command should look somehow like this:

print "%.5f %.5f %.5f %.5f %.5f %.5f" % (*A, *B)

Upvotes: 0

Views: 44

Answers (1)

falsetru
falsetru

Reputation: 368954

% operator - string formatting operation can accept a tuple as a argument:

>>> A = [1,2,3]
>>> B = [5,6,7]
>>> "%.5f %.5f %.5f %.5f %.5f %.5f" % tuple(A + B)
'1.00000 2.00000 3.00000 5.00000 6.00000 7.00000'

Upvotes: 1

Related Questions