更改默认浮动打印格式
我有一些列表和包含浮点数的更复杂的结构。打印它们时,我看到带有很多十进制数字的浮点数,但打印时,我不需要全部。 所以我想在打印浮点数时定义自定义格式(例如2或3位小数)。
我需要使用浮点数而不是十进制。另外,我不允许截断/舍入浮点数。
有没有办法改变默认行为?
I've some lists and more complex structures containing floats. When printing them, I see the floats with a lot of decimal digits, but when printing, I don't need all of them.
So I would like to define a custom format (e.g. 2 or 3 decimals) when floats are printed.
I need to use floats and not Decimal. Also, I'm not allowed to truncate/round floats.
Is there a way to change the default behavior?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
正如 Ignacio 所说,你不可以对 C 类型进行猴子补丁。
但是,如果您迫于压力并且了解一些 C,您可以自己修改 Python 解释器源代码,然后将其重新编译为自定义解决方案。有一次我修改了列表的标准行为之一,这只是一种中等程度的痛苦。
我建议您找到更好的解决方案,例如仅使用
"%0.2f"
printf 表示法打印浮点数:或
You are not allowed to monkeypatch C types, like Ignacio said.
However, if you are terribly pressed in doing so and you know some C, you could go modify the Python interpreter source code yourself, then recompile it into a custom solution. Once I modified one of the standard behaviors for lists and it was only a moderate pain.
I suggest you find a better solution, such as just printing the floats with the
"%0.2f"
printf notation:or
这并不能回答嵌套在其他结构中的浮点数的更普遍的问题,但如果您只需要在列表甚至类似数组的嵌套列表中打印浮点数,请考虑使用 numpy。
例如,
给出
This doesn't answer the more general question of floats nested in other structures, but if you just need to print floats in lists or even array-like nested lists, consider using
numpy
.e.g.,
gives
从 Python 文档 中,
repr(a)
会给出 17数字(只需在交互式提示中输入a
即可看到,但str(a)
(打印时自动执行)四舍五入为 12。编辑:大多数基本的黑客解决方案...
不过你必须使用你自己的类,所以......是的。
From the Python docs,
repr(a)
would give 17 digits (as seen by just typinga
at the interactive prompt, butstr(a)
(automatically performed when you print it) rounds to 12.Edit: Most basic hack solution...
You have to use your own class though, so...yeah.
不可以,因为这需要修改
float.__str__()
,但不允许您对 C 类型进行 Monkeypatch。请改用字符串插值或格式化。No, because that would require modifying
float.__str__()
, but you aren't allowed to monkeypatch C types. Use string interpolation or formatting instead.我今天遇到了这个问题,我想出了一个不同的解决方案。如果您担心打印时的样子,可以将 stdout 文件对象替换为自定义文件对象,当调用 write() 时,该对象会搜索任何看起来像浮点数的内容,并将它们替换为您自己的格式他们。
如果您只是将数字放入字符串中,那是没有好处的,但最终您可能希望将该字符串写入某处的某种文件中,并且您可以使用上述对象包装该文件。显然,这会带来一些性能开销。
公平警告:我还没有在 Python 3 中测试过这个,我不知道它是否有效。
I ran into this issue today, and I came up with a different solution. If you're worried about what it looks like when printed, you can replace the stdout file object with a custom one that, when write() is called, searches for any things that look like floats, and replaces them with your own format for them.
It's no good if you're just putting numbers into a string, but eventually you'll probably want to write that string to some sort of file somewhere, and you may be able to wrap that file with the above object. Obviously there's a bit of a performance overhead.
Fair warning: I haven't tested this in Python 3, I have no idea if it would work.
对于那些在pandas中工作的人:
pd.options.display.float_format = "{:,.0f}".format
我知道这是一篇“旧”帖子,但是当我快速搜索网络以找到此解决方案时,我经常会得到较旧的“解决方法”。如果我完全错过了什么,请提前道歉。
上面的代码将所有浮点数格式化为带有“千”分隔符的整数。其他小数位可以通过更改格式规范字符串来获得。
健康警告 - 这会使调试变得复杂,因为所有输出(例如“打印”、“显示”和“描述”)也会输出数字适当地圆形。当某些浮点数实际上是不同的值时,它们会以不同的方式显示(即四舍五入),这可能会出现问题。
For those working in pandas:
pd.options.display.float_format = "{:,.0f}".format
I know this is an "old" post, but when I quickly search the web to find this solution, I often get older "work-arounds". Apologies in advance if I've totally missed something.
The above code formats all floats as whole numbers with "thousands" separator. Other decimal places can be obtained from changing the format specification string.
Health warning - this can complicate debugging as all output (e.g., "print", "display" and "describe") will also output figures duly rounded. This can be problematic when some floats will display differently (i.e., rounded) when they are actually a different value.
搭载为 sympy 编写的函数(请参阅此 ),我整理了一种方法来实现这一目标。
使用下面的两个函数,将每个
print(...
替换为print_my(...
)。在我看来,这比大多数函数更简单、更易于使用且用途广泛发布了其他解决方案。
Piggybacking on a function written for sympy (see this), I have put together a way to achieve this.
With the two functions below, replace every
print(...
withprint_my(...
.This seems to me exceedingly less contrived, easier to use, and versatile than most other solutions posted.
升级到Python 3.1。它不会使用不必要的数字。
Upgrade to Python 3.1. It doesn't use more digits than necessary.
如果你使用C语言,你可以使用
#define
或
"%*.*f"
来做到这一点,例如If you are using C language, you can either use
#define
or
"%*.*f"
to do that, e.g.