LWZ*_*LWZ 4 python string floating-point python-2.7
我有一个浮点数元组的列表,类似于
[ (1.00000001, 349183.1430, 2148.12222222222222), ( , , ), ..., ( , ,) ]
Run Code Online (Sandbox Code Playgroud)
如何将所有数字转换为字符串,具有相同的格式(科学记数法,精确到8位小数),同时保持相同的结构(元组列表或列表列表)?
我想我可以用嵌套for循环来做,但是有一种更简单的方法,比如使用map某种方式?
假设您有一些列表列表或元组列表:
lst = [ [ 1,2,3 ], [ 1e6, 2e6, 3e6], [1e-6, 2e-6, 3e-6] ]
Run Code Online (Sandbox Code Playgroud)
您可以使用列表解析创建并行列表列表:
str_list = [['{0:.8e}'.format(flt) for flt in sublist] for sublist in lst]
Run Code Online (Sandbox Code Playgroud)
或者是一个元组列表:
str_list = [tuple('{0:.8e}'.format(flt) for flt in sublist) for sublist in lst]
Run Code Online (Sandbox Code Playgroud)
然后,如果您想显示这组数字:
str_display = '\n'.join(' '.join(lst) for lst in strlist)
print str_display
Run Code Online (Sandbox Code Playgroud)