Neeraj Hanumante
Neeraj Hanumante

Reputation: 1684

Python: print list of numbers in scientific notation

Background:

local_print = [0.03, 535, 7]

This can be printed in scientific notation using following

for x in local_print:
    print('{:.3e}'.format(x))

Without scientific notation this can be printed as follows:

print(*local_print, sep='\t')

Question

Is there any way to combine these two printing methods? I want to print using

print(*local_print, sep='\t')

in scientific format.

Upvotes: 2

Views: 2745

Answers (3)

Sin Han Jinn
Sin Han Jinn

Reputation: 684

If you want a more lay man way, just print from a different list.

scientific = []
for x in local_print:
    scientific.append('{:.3e}'.format(x))
print(*scientific, sep='\t')

Output:

3.000e-02      5.350e+02      7.000e+00

Upvotes: 1

kellymandem
kellymandem

Reputation: 1769

You can also use a list comprehension

local_print = [0.03, 535, 7]
print('\t'.join(['{:.3e}'.format(x) for x in local_print]))

Upvotes: 2

Selcuk
Selcuk

Reputation: 59219

The usual way is to use a generator expression:

 print(*('{:.3e}'.format(x) for x in local_print), sep='\t')

Upvotes: 1

Related Questions