我编写了一个解析一些文本文档的代码,并显示了某些单词出现的频率。
输出如下:
Counting words by tag
---------------------
modals
per 1000 words can could shall should will would
austen-emma.txt 1.480000 4.350000 1.100000 1.920000 2.900000 4.260000
austen-persuasion.txt 1.090000 4.590000 0.560000 1.920000 1.650000 3.620000
austen-sense.txt 1.500000 4.060000 0.870000 1.640000 2.500000 3.600000
chesterton-ball.txt 1.460000 1.210000 0.510000 0.770000 2.040000 1.440000
chesterton-brown.txt 1.500000 1.990000 0.350000 0.650000 1.290000 1.570000
chesterton-thursday.txt 1.760000 2.180000 0.710000 0.780000 1.570000 1.730000
正如您所看到的,数字四舍五入到百分之一,但我的程序仍会打印一些过零的数字。
关于制表的数据,即以下代码行:
cell = round(float(cfdist[fileid][w])*1000/number_of_words[fileid],2)
print '%6f' % (cell),
有人能够帮我改进行打印'%6f'%(单元格),以便它不会显示结束零点吗?
谢谢!
答案 0 :(得分:3)
请参阅Python string formatting文档。 “%6f”是你的问题。相反,尝试
print "%.2f" % cell
答案 1 :(得分:1)
num = 12.3456
new_val ='%。2f'%float(num)
print float(new_val)
12.34