Python 控制混合列表打印输出中的小数数(pprint.pformat、json.dumps)

Python 控制混合列表打印输出中的小数数(pprint.pformat、json.dumps),python,formatting,Python,Formatting,我有一个“混合列表”(这里的意思是,一个可能包括列表、dict、string、int或float的列表),我想打印它——也就是说,获取它的字符串表示形式,希望是一个“漂亮”的表示形式——但是,以这样的方式,这个数据结构中的float的小数数是有限的。然后,原则上,我可能希望将这个字符串保存到一个文件中,然后再次加载它 根据经验,我希望绝对值>0.01的所有值仅用两位小数格式化,其余值用科学记数法格式化 查看一些SO帖子,我成功地给出了以下示例(在MSYS2、Windows 10上与Python

我有一个“混合列表”(这里的意思是,一个可能包括列表、dict、string、int或float的列表),我想打印它——也就是说,获取它的字符串表示形式,希望是一个“漂亮”的表示形式——但是,以这样的方式,这个数据结构中的float的小数数是有限的。然后,原则上,我可能希望将这个字符串保存到一个文件中,然后再次加载它

根据经验,我希望绝对值>0.01的所有值仅用两位小数格式化,其余值用科学记数法格式化

查看一些SO帖子,我成功地给出了以下示例(在MSYS2、Windows 10上与Python 2.7.16和Python 3.7.4配合使用):

#/usr/bin/env python
输入数学
导入pprint
# https://stackoverflow.com/questions/1447287/format-floats-with-standard-json-module
导入json
从json导入编码器
编码器.FLOAT_REPR=lambda o:格式(o,'.2f')
# https://stackoverflow.com/questions/1447287/format-floats-with-standard-json-module
def圆形_浮动(o):
if isinstance(o,float):如果abs(o)>0.01,则返回“{.2f}”.format(o),否则返回“{.2e}”.format(o)
if isinstance(o,dict):返回{k:round_float(v)表示k,v在o.items()中
if isinstance(o,(list,tuple)):返回[round_浮动(x)表示o中的x]
返回o
导入集合
试试看https://stackoverflow.com/questions/53978542/how-to-use-collections-abc
导入集合.abc
collectionsAbc=collections.abc
除(导入错误、属性错误)外,如e:
集合ABC=集合
进口编号
# https://stackoverflow.com/questions/7076254/rounding-decimals-in-nested-data-structures-in-python
def fpformat(thing,formatfunc):
如果存在(事物、指令):
试试看:#Python 2
thingiter=thing.iteritems()
除了:#Python 3
thingiter=thing.items()
返回类型(thing)((key,fpformat(value,formatfunc))用于key,thingite中的value)
如果是实例(对象、集合ABC.Container):
返回类型(thing)(thing中的值的fpformat(value,formatfunc)
如果存在(事物、数字、数字):
返回formatfunc(thing)
归还物
def格式浮动(对象):
返回“%.3g”%float(thing)
#############
#制作一个源数组,混合数据
tarr=[
[“aa”、“bb”、“cc”、“dd”、“ee”],
[{'v':1.1},{'w':2.2},{'x':3.3},{'y':4.4},{'z':5.5555}],
[ 10,          20,          30,          40,          50            ],
[ 11.1,        22.22,       33.333,      44.4444,     55.55555      ]
]
#再创建一些小数:
appendrow=[]
对于ind,枚举中的tnum(tarr[2]):
tpnum=((ind+1.0)/(ind+2.0))*math.pi*tnum
appendrow.append(tpnum)
tarr.append(appendrow)
appendrow=[]
对于ind,枚举中的tnum(tarr[2]):
tpnum=((ind+1.0)/(ind+2.0))*math.pi*tnum/100000.0
appendrow.append(tpnum)
tarr.append(appendrow)
tarr\u ppf\u string=pprint.pformat(tarr)
打印(“打印输出1:\n{}\n”.format(tarr\u ppf\u字符串))
tarr\u ppf\u string2=pprint.pformat(圆形浮点数(tarr))
打印(“打印输出2:\n{}\n”.format(tarr\u ppf\u string2))
tarr\u json\u string=json.dumps(tarr)
打印(“打印输出3:\n{}\n.format(tarr\u json\u字符串))
tarr_json_string2=json.dumps(圆形浮点数(tarr))
打印(“打印输出4:\n{}\n.format(tarr\u json\u string2))
tarr\u fp\u string=fpformat(tarr,formatfloat)
打印(“打印输出5:\n{}\n.format(tarr\u fp\u字符串))
Python 3中此脚本的输出如下:

打印输出1:
[aa',bb',cc',dd',ee'],
[{'v':1.1},{'w':2.2},{'x':3.3},{'y':4.4},{'z':5.5555}],
[10, 20, 30, 40, 50],
[11.1, 22.22, 33.333, 44.4444, 55.55555],
[15.707963267948966,
41.8879020478639,
70.68583470577035,
100.53096491487338,
130.89969389957471],
[0.00015707963267948965,
0.00041887902047863906,
0.0007068583470577034,
0.0010053096491487337,
0.0013089969389957472]]
打印输出2:
[aa',bb',cc',dd',ee'],
[{'v':'1.10'},{'w':'2.20'},{'x':'3.30'},{'y':'4.40'},{'z':'5.56'},
[10, 20, 30, 40, 50],
['11.10', '22.22', '33.33', '44.44', '55.56'],
['15.71', '41.89', '70.69', '100.53', '130.90'],
['1.57e-04','4.19e-04','7.07e-04','1.01e-03','1.31e-03']
打印输出3:
[aa]、[bb]、[cc]、[dd]、[ee]、[v:1.1}、[w:2.2}、[x:3.3}、[y:4.4}、[z:5.5555}]、[10,20,30,40,50]、[11.1,22.22,33.333,44.4444,55.5555555]、[15.707963267948966,41.887902047639,70.68583470577035,100.53096497338,130.899371]、[,[0.00015707963267948965, 0.00041887902047863906, 0.0007068583470577034, 0.0010053096491487337, 0.0013089969389957472]]
打印输出4:
[aa]、[bb]、[cc]、[dd]、[ee]、[1.10]、[w]、[2.20]、[x]、[3.30]、[y]、[4.40]、[z]、[5.56]、[10、20、30、40、50]、[11.10”、“22.22”、“33.33”、“44.44”、“55.56”]、[15.71”、“41.89”、“70.69”、“100.53”、“130.90”]、[1.57e-04”、“4.19e-04”、“7.07e-01”、“E-03”]
打印输出5:
[1.1]、[w]、[2.2]、[x]、[3.3]、[y]、[z]、[5.56]、[10]、[20]、[30]、[40]、[50]、[11.1]、[22.2]、[33.3]、[44.4]、[15.7]、[41.9]、[70.7]、[101]、[0.000419]、[0.000707]、[0.00131]、[0.00131]
从本质上说,我想要的是“打印输出2”-除了数字剩余的数字之外,不打印为字符串;也就是说,我希望打印输出是这样的:

['aa',bb',cc',dd',ee'],
[{'v':1.1'},{'w':2.20},{'x':3.30},{'y':4.40},{'z':5.56}],
[10, 20, 30, 40, 50],
[11.10, 22.22, 33.33, 44.44, 55.56],
[15.71, 41.89, 70.69, 100.53, 130.90],
[1.57e-04、4.19e-04、7.07e-04、1.01e-03、1.31e-03]]
如何在Python中实现这种打印输出?(Python 3需要这种打印输出,但Python 2的解决方案也很好)

旧答案 问题是,您正在将浮点数作为字符串而不是浮点数插入。您正在打印包含字符串的词典,因此它们被打印为字符串。您希望将数字作为浮点数插入

你可以柔顺
printout 2:
[['aa', 'bb', 'cc', 'dd', 'ee'],
 [{'v': 1.1}, {'w': 2.2}, {'x': 3.3}, {'y': 4.4}, {'z': 5.56}],
 [10, 20, 30, 40, 50],
 [11.1, 22.22, 33.33, 44.44, 55.56],
 [15.71, 41.89, 70.69, 100.53, 130.9],
 [0.0, 0.0, 0.0, 0.0, 0.0]]

[['aa', 'bb', 'cc', 'dd', 'ee'],
 [{'v': 1.1}, {'w': 2.2}, {'x': 3.3}, {'y': 4.4}, {'z': 5.56}],
 [10, 20, 30, 40, 50],
 [11.1, 22.2, 33.3, 44.4, 55.6],
 [15.7, 41.9, 70.7, 101.0, 131.0],
 [0.000157, 0.000419, 0.000707, 0.00101, 0.00131]]
 #Note that the bottom row is badly represented, but this representation is
 #not longer than writing out the same number in scientific notation. If
 #These numbers were smaller, they would be represented scientifically.