[英]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.以科学的形式。
You can also use a list comprehension您还可以使用列表推导
local_print = [0.03, 535, 7]
print('\t'.join(['{:.3e}'.format(x) for x in local_print]))
The usual way is to use a generator expression:通常的方法是使用生成器表达式:
print(*('{:.3e}'.format(x) for x in local_print), sep='\t')
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: Output:
3.000e-02 5.350e+02 7.000e+00
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.