摆脱python中的零[重复](Get rid of zeros in python [duplicate])

编程入门 行业动态 更新时间:2024-10-25 08:26:40
摆脱python中的零[重复](Get rid of zeros in python [duplicate])

这个问题在这里已有答案:

将浮动限制为两个小数点 19个答案

我在Python中编写了一个非常简单的代码。 我需要以0.1步长打印1到5之间的所有数字(包括1和5 )。 整数应该没有后缀.0 ,即输出应该是:

0 0.1 0.2 ... 1 1.1 ... ... 5

这是我的代码:

for i in range(0,51): if i%10==0: z=int(i/10) print (z) else: print (i*0.1)

这是我的输出:

0 0.1 0.2 0.30000000000000004 0.4 0.5 0.6000000000000001 0.7000000000000001 0.8 0.9 1 1.1 1.2000000000000002 1.3 1.4000000000000001 1.5 1.6 1.7000000000000002 1.8 1.9000000000000001 2 2.1 2.2 2.3000000000000003 2.4000000000000004 2.5 2.6 2.7 2.8000000000000003 2.9000000000000004 3 3.1 3.2 3.3000000000000003 3.4000000000000004 3.5 3.6 3.7 3.8000000000000003 3.9000000000000004 4 4.1000000000000005 4.2 4.3 4.4 4.5 4.6000000000000005 4.7 4.800000000000001 4.9 5

我不需要所有这些零.000000001 。

This question already has an answer here:

Limiting floats to two decimal points 24 answers

I wrote a very simple code in Python. I need to print all the numbers between 1 and 5 (including 1 and 5) in steps of 0.1. The integers should be without the suffix .0, i.e. the output should be:

0 0.1 0.2 ... 1 1.1 ... ... 5

Here is my code:

for i in range(0,51): if i%10==0: z=int(i/10) print (z) else: print (i*0.1)

Here is my output:

0 0.1 0.2 0.30000000000000004 0.4 0.5 0.6000000000000001 0.7000000000000001 0.8 0.9 1 1.1 1.2000000000000002 1.3 1.4000000000000001 1.5 1.6 1.7000000000000002 1.8 1.9000000000000001 2 2.1 2.2 2.3000000000000003 2.4000000000000004 2.5 2.6 2.7 2.8000000000000003 2.9000000000000004 3 3.1 3.2 3.3000000000000003 3.4000000000000004 3.5 3.6 3.7 3.8000000000000003 3.9000000000000004 4 4.1000000000000005 4.2 4.3 4.4 4.5 4.6000000000000005 4.7 4.800000000000001 4.9 5

I don't need all this zeros .000000001.

最满意答案

利用Python的字符串格式化功能,您可以将整个循环减少到一行:

for i in range(0, 51): print('{:.{deci}f}'.format(i*0.1, deci=min(1, i%10)))

输出:

0 0.1 0.2 ... 0.8 0.9 1 1.1 1.2 ... 1.8 1.9 2 2.1 2.2 ... 4.8 4.9 5

说明

这给你0为i倍数为10,其他为1倍数:

>>> i = 10 >>> min(1, i%10) 0 >>> i = 8 >>> min(1, i%10) 1

现在,您可以将此用作关键字参数deci来确定输出中的小数位数:

>>> '{:.{deci}f}'.format(1.99, deci=1) '2.0' >>> '{:.{deci}f}'.format(1.99, deci=0) '2'

Taking advantage of Python's string formatting capabilities, you can reduce your whole loop to one line:

for i in range(0, 51): print('{:.{deci}f}'.format(i*0.1, deci=min(1, i%10)))

Output:

0 0.1 0.2 ... 0.8 0.9 1 1.1 1.2 ... 1.8 1.9 2 2.1 2.2 ... 4.8 4.9 5

Explanation

This gives you 0 for multiples of 10 for i and 1 for others:

>>> i = 10 >>> min(1, i%10) 0 >>> i = 8 >>> min(1, i%10) 1

Now, you can use this for the keyword argument deci to determine the number of decimals in your output:

>>> '{:.{deci}f}'.format(1.99, deci=1) '2.0' >>> '{:.{deci}f}'.format(1.99, deci=0) '2'

更多推荐

本文发布于:2023-07-24 10:55:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1245027.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:rid   python   duplicate   zeros

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!