为什么以下循环不断返回0的结果?

时间:2013-10-25 21:18:45

标签: python loops

所以我试图生成一个频谱,为了做到这一点,我试图将我的数据分类到一个列中,这个列是我附加的数据,名为minorallelefreq。由于某种原因,代码无法正常工作,因为我为所有5个bin获取值为0。

以下是代码:

minprop = 0
minprop1 = 0
minprop2 = 0
minprop3 = 0
minprop4 = 0 

for x in range(1,100):

    if minorallelefreq[x] <= 0.1:
        minprop = minprop + 1 
    if minorallelefreq[x] > 0.1 and minorallelefreq[x] <= 0.2: 
        minprop1 = minprop1 + 1 
    if minorallelefreq[x] > 0.2 and minorallelefreq[x] <= 0.3: 
        minprop2 = minprop2 + 1
    if minorallelefreq[x] > 0.3 and minorallelefreq[x] <= 0.4: 
        minprop3 = minprop3 + 1
    if minorallelefreq[x] > 0.4 and minorallelefreq[x] <= 0.5:
        minprop4 = minprop4 + 1



bin1 = minprop/float(counter)
bin2 = minprop1/float(counter)
bin3 = minprop2/float(counter)
bin4 = minprop3/float(counter)
bin5 = minprop4/float(counter)  
print "Bin1 (0-0.1)=", bin1, "\t", "Bin2 (0.1-0.2)=", bin2, "\t", "Bin3 (0.2-0.3)=", bin3, "\t", "Bin4 (0.3-0.4)=", bin4, "\t", "Bin5 (0.4-0.5)=", bin5

所以事实证明循环不起作用的原因是因为python没有读取我的值(全部为小数)作为小数。所以,我不得不将其更改为float(minorallelefreq [x]),并且它有效。

2 个答案:

答案 0 :(得分:4)

此代码中有几个可能的错误

  1. int(0.1)=&gt; 0,所以minprop将始终为0,除非有负值
  2. minprop4没有缩进,并且永远不会被设置,因为该值不能同时为&gt; 0.4和<= 0.4
  3. 您假设有100个元素,并且它们都在0到0.4之间
  4. 我建议您尝试根据实际值而不是预期值自动bucketize:

    import collections
    buckets = collections.Counter()
    
    for value in minorallfreq:
        bucket = int(value * 10) / 10.0
        buckets[bucket] += 1
    

答案 1 :(得分:2)

if minorallelefreq[x] <= int(0.1):
    minprop = minprop + 1 
if minorallelefreq[x] > 0.1 and minorallelefreq[x] <= 0.2: 
    minprop1 = minprop1 + 1 

表示不会对值采取任何操作&gt; 0和&lt; = 0.1。如果您删除int()来电会怎么样?

此外,值不会发生任何事情&gt; 0.4:

if minorallelefreq[x] > 0.4 and minorallelefreq[x] <= 0.4:  # never True
    minprop4 = minprop4 + 1                                 # indentation??

通常,您应该查看链式比较:

if 0.1 < minorallelefreq[x] <= 0.2:   # much easier to read, and faster.
    # etc.