我有一组浮点数(总是小于0)。我想把它放入柱状图中, i、 e.直方图中的每个条形图都包含值范围[0,0.150]
我得到的数据如下:
0.000
0.005
0.124
0.000
0.004
0.000
0.111
0.112
我希望下面的代码能得到如下结果
[0, 0.005) 5
[0.005, 0.011) 0
...etc..
我试着用我的密码来做这种事。 但似乎没用。正确的方法是什么?
#! /usr/bin/env python
import fileinput, math
log2 = math.log(2)
def getBin(x):
return int(math.log(x+1)/log2)
diffCounts = [0] * 5
for line in fileinput.input():
words = line.split()
diff = float(words[0]) * 1000;
diffCounts[ str(getBin(diff)) ] += 1
maxdiff = [i for i, c in enumerate(diffCounts) if c > 0][-1]
print maxdiff
maxBin = max(maxdiff)
for i in range(maxBin+1):
lo = 2**i - 1
hi = 2**(i+1) - 1
binStr = '[' + str(lo) + ',' + str(hi) + ')'
print binStr + '\t' + '\t'.join(map(str, (diffCounts[i])))
~
如果可能的话,不要重新发明轮子。努比有你需要的一切:
第一个错误是:
当需要str时,为什么要将int转换为str?解决这个问题,然后我们得到:
因为你只做了5桶。我不明白你的骗局,但让我们做50桶看看会发生什么:
maxdiff
是int列表中的一个值,那么max
在这里做什么呢?移除它,现在我们得到:果然,您使用了一个值作为
map
的第二个参数。让我们简化一下最后两行:对此:
现在它打印:
我不知道在这里还能做什么,因为我真的不明白你想用什么。它似乎包含二元力量,但对我来说没有意义。。。
相关问题 更多 >
编程相关推荐