tags:

views:

67

answers:

4
  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?

+2  A: 

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.

Andre Holzner
when i want a float divide it's always x / float(y); but as ints and floats aren't objects they don't have a class but they do have a type which can be tested with say type(x)
Dan D
@Dan, ints are type `int`, floats are type `float` - types are classes. and ints and floats are objects!
gnibbler
Or try `from __future__ import division` to get exact results from `/`
S.Lott
+2  A: 

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.

Mark_Masoul
+3  A: 

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]
gnibbler
+5  A: 

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

dan04