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

2024-06-29 00:18:31 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

通过查看一些SO帖子,我成功地给出了以下示例(在MSYS2和Windows 10上使用Python2.7.16和Python3.7.4):

#!/usr/bin/env python

import math
import pprint

# https://stackoverflow.com/questions/1447287/format-floats-with-standard-json-module
import json
from json import encoder
encoder.FLOAT_REPR = lambda o: format(o, '.2f')

# https://stackoverflow.com/questions/1447287/format-floats-with-standard-json-module
def round_floats(o):
  if isinstance(o, float): return "{:.2f}".format(o) if abs(o)>0.01 else "{:.2e}".format(o)
  if isinstance(o, dict): return {k: round_floats(v) for k, v in o.items()}
  if isinstance(o, (list, tuple)): return [round_floats(x) for x in o]
  return o

import collections
try: # https://stackoverflow.com/questions/53978542/how-to-use-collections-abc
  import collections.abc
  collectionsAbc = collections.abc
except (ImportError, AttributeError) as e:
  collectionsAbc = collections
import numbers

# https://stackoverflow.com/questions/7076254/rounding-decimals-in-nested-data-structures-in-python
def fpformat(thing, formatfunc):
  if isinstance(thing, dict):
    try: # Python 2
      thingiter = thing.iteritems()
    except: # Python 3
      thingiter = thing.items()
    return type(thing)((key, fpformat(value, formatfunc)) for key, value in thingiter)
  if isinstance(thing, collectionsAbc.Container):
    return type(thing)(fpformat(value, formatfunc) for value in thing)
  if isinstance(thing, numbers.Number):
    return formatfunc(thing)
  return thing
def formatfloat(thing):
  return "%.3g" % float(thing)

#############

# make a source array, mixed data

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      ]
]

# create some more decimals:
appendrow = []

for ind, tnum in enumerate(tarr[2]):
  tpnum = ((ind+1.0)/(ind+2.0))*math.pi*tnum
  appendrow.append(tpnum)

tarr.append(appendrow)

appendrow = []

for ind, tnum in enumerate(tarr[2]):
  tpnum = ((ind+1.0)/(ind+2.0))*math.pi*tnum/100000.0
  appendrow.append(tpnum)

tarr.append(appendrow)

tarr_ppf_string = pprint.pformat(tarr)

print("printout 1:\n{}\n".format(tarr_ppf_string))

tarr_ppf_string2 = pprint.pformat(round_floats(tarr))

print("printout 2:\n{}\n".format(tarr_ppf_string2))

tarr_json_string = json.dumps(tarr)

print("printout 3:\n{}\n".format(tarr_json_string))

tarr_json_string2 = json.dumps(round_floats(tarr))

print("printout 4:\n{}\n".format(tarr_json_string2))

tarr_fp_string = fpformat(tarr, formatfloat)

print("printout 5:\n{}\n".format(tarr_fp_string))

Python 3中此脚本的输出如下:

printout 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]]

printout 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']]

printout 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.55555], [15.707963267948966, 41.8879020478639, 70.68583470577035, 100.53096491487338, 130.89969389957471], [0.00015707963267948965, 0.00041887902047863906, 0.0007068583470577034, 0.0010053096491487337, 0.0013089969389957472]]

printout 4:
[["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"]]

printout 5:
[['<generator object fpformat.<locals>.<genexpr> at 0x6ffffcc57d0>', '<generator object fpformat.<locals>.<genexpr> at 0x6ffffcc57d0>', '<generator object fpformat.<locals>.<genexpr> at 0x6ffffcc57d0>', '<generator object fpformat.<locals>.<genexpr> at 0x6ffffcc57d0>', '<generator object fpformat.<locals>.<genexpr> at 0x6ffffcc57d0>'], [{'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', '131'], ['0.000157', '0.000419', '0.000707', '0.00101', '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中实现这种打印输出(Python3需要这个,但Python2的解决方案也很好)


Tags: inimportjsonformatforstringreturnif
1条回答
网友
1楼 · 发布于 2024-06-29 00:18:31

旧答案

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

您可以将浮点数舍入到一定的小数位数,而无需将它们转换为字符串

def round_floats(o):
  if isinstance(o, float): return round(o, 2) #Line 13, using round instead of
                                                  #string formatting
  if isinstance(o, dict): return {k: round_floats(v) for k, v in o.items()}
  if isinstance(o, (list, tuple)): return [round_floats(x) for x in o]
  return o

round(float, decimals)函数替换字符串格式的使用将为printout2提供以下输出:

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]]


新答案

编辑-经过多次调试,我们偶然发现了一点问题。不可能强迫漂亮的印刷品一直使用某种指数格式

我尝试使用this位代码覆盖漂亮打印机的float操作符,但它不适用于列表。如果类型嵌套在列表/字典/结构中,则此解决方案不会重写该类型的格式化程序。不幸的是,如果不重新编写一半漂亮的打印机代码,这个解决方案似乎不可行

好消息是可能没有必要。你可以只使用精度的小数点后两位。这并不能保证用科学记数法来表示数字,但在大多数情况下这将适合您

def round_floats(o):
  if isinstance(o, float): return float("{:.2f}".format(o) if abs(o)>0.01 else "{:.2e}".format(o))
  #Edited line 13, just casting back to float
  if isinstance(o, dict): return {k: round_floats(v) for k, v in o.items()}
  if isinstance(o, (list, tuple)): return [round_floats(x) for x in o]

最好使用decimal类来调整数字的精度

import decimal
decimal.getcontext().prec = 3

def round_floats(o):
  if isinstance(o, float): return float(+decimal.Decimal(o))
  if isinstance(o, dict): return {k: round_floats(v) for k, v in o.items()}
  if isinstance(o, (list, tuple)): return [round_floats(x) for x in o]

不管是哪种情况,坏消息是0左右的数字的行为都不是你想要的。像0.0001这样的数字将保持相同的表示形式(与1.0e-4相反)。然而,它确实执行计算并检查哪个符号(科学的或正常的)占用较少的空间,因此给出这种方法,每个表示都保证是尽可能短的

输出:

[['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.

相关问题 更多 >