问题描述
我得到的百分比输出如下:
代码:
current = df['August']
prevIoUs = df['September']
per=[]
for a,b in zip(current,prevIoUs):
try:
per.append(round((((a - b) / a) * 100.0)))
except ZeroDivisionError:
per.append(0)
[0,25,54,-22,100,38,-117,1,-377,37]
期望输出以及“%”符号,例如:
[0%,25%,54%,0%,-22%,100%,38%,-117%,1%,-377%,37%]
解决方法
如果您需要在float
中添加“%”,则需要将其转换为str
所以,我的建议是:
for a,b in zip(current,previous):
try:
#Add this two new lines
x = repr((a - b) / a * 100.0)
per.append(x + "%")
#Do not need this
#per.append(round((((a - b) / a) * 100.0)))
except ZeroDivisionError:
per.append(0)
打印出print(per)
产量:
['50.0%']