开发者

python: appends only '0'

  big_set=[]
  for i in results开发者_如何学编程_histogram_total:
    big_set.append(100*(i/sum_total))

big_set returns [0,0,0,0,0,0,0,0........,0]

this wrong because i checked i and it is >0

what am i doing wrong?


In Python 2.x, use from __future__ import division to get sane division behavior.


try this list comprehension instead

big_set = [100*i/sum_total for i in results_histogram_total]

note that / truncates in Python2, so you may wish to use

big_set = [100.0*i/sum_total for i in results_histogram_total]


If sum_total is an integer (what is sum_total.__class__ equal to ?), python seems to use integer division.

Try i / float(sum_total) instead.


Probably has to do with float division.

i is probably less than sum_total which in integer division returns 0.

100 * 0 is 0.

Try casting it to a float.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜