为什么我的函数遍历列表(Python)?

时间:2015-07-15 18:03:33

标签: python function

我不确定它是在迭代列表还是被多次调用......在任何一种情况下它都不应该做这两件事。我对python和编程很新。这是功能:

"""
    Gets the mass that may have an isotope of +1 neutron for C,N or O (if      there is one)  
   @param mass     the mass
   @return  the mass +1
"""
def getMassPlus(mass):
    if (mass)+1 in mass_old2:
        return (mass)+1
    elif (mass)+1.1 in mass_old2:
        return (mass)+1.1
    elif (mass)+1.2 in mass_old2:
       return (mass)+1.2
    elif (mass)+.9 in mass_old2:
       return (mass)+.9

这是函数调用:

if ((14.00674*multiplyByN)+(15.994915*multiplyByO)+(1.00794*multiplyByH)) == isotope_masses[i]+.5 or isotope_masses[i]-.5: 
      if isotope_masses[i]+1 or isotope_masses[i]+1.1 or isotope_masses[i]+1.2 or isotope_masses[i]+.9 in mass_old2:
        nextMass = getMassPlus(isotope_masses[i])

当我调用该函数时,它会以某种方式遍历列表isotope_masses(看起来像这样:isotope_masses = [1,2,3,4,...])十次并将nextMass十个值分配为好。它也可能有助于您知道第二个代码块是一个循环遍历isotope_masses长度的更大循环的一部分。我不确定为什么会发生这个问题,任何想法都会受到赞赏。提前谢谢。

循环:

for i in range(len(isotope_masses)):
    if not noNitrogen(molecules[i]):   #molecule has nitrogen and possibly hydrogen
    if hasDoubleDig('N'):
        multiplyByN = int(molecules[i][3:5])
        multiplyByH = int(molecules[i][8:])
    else:
        multiplyByN = int(molecules[i][3])
        multiplyByH = int(molecules[i][7:])
    if ((14.00674*multiplyByN)+(1.00794*multiplyByH)) == isotope_masses[i]+.5 or isotope_masses[i]-.5:
        if isotope_masses[i]+1 or isotope_masses[i]+1.1 or isotope_masses[i]+1.2 or isotope_masses[i]+.9 in mass_old2:
            nextMass = getMassPlus(isotope_masses[i])
            if float(intensities[mass_old2.index(nextMass)])/float(intensities[mass_old2.index(isotopes_masses[i])]) == multiplyByN * .00364:
                file_isotopes.append("Isotope: is N-15") 
            else:
                file_isotopes.append("Mix of isotopes")
    elif not noCarbon(molecules[i]):   #molecule has carbon and possibly hydrogen
    if hasDoubleDig('C'):
        multiplyByC = int(molecules[i][1:3])
        multiplyByH = int(molecules[i][8:])
    else:
        multiplyByC = int(molecules[i][1])
        multiplyByH = int(molecules[i][7:])
    if ((12.0107*multiplyByC)+(1.00794*multiplyByH)) == isotope_masses[i]+.5 or isotope_masses[i]-.5:
        if isotope_masses[i]+1 or isotope_masses[i]+1.1 or isotope_masses[i]+1.2 or isotope_masses[i]+.9 in mass_old2:

            print isotope_masses[i]

            nextMass = getMassPlus(isotope_masses[i])
            if float(intensities[mass_old2.index(nextMass)])/float(intensities[mass_old2.index(isotope_masses[i])]) == multiplyByC * .0107:
                file_isotopes.append("Isotope is: C-13")
            else:
                file_isotopes.append("Mix of isotopes")

循环还有更多,但它只是重复,所以为了简洁起见,我只在循环中给出了两个案例。

2 个答案:

答案 0 :(得分:0)

我认为你的if语句总是等于True。

而不是:

if ((14.00674*multiplyByN)+(15.994915*multiplyByO)+(1.00794*multiplyByH)) == isotope_masses[i]+.5 or isotope_masses[i]-.5

你需要条件的第二部分也是一个完整的比较:

if ((14.00674*multiplyByN)+(15.994915*multiplyByO)+(1.00794*multiplyByH)) == isotope_masses[i]+.5 or ((14.00674*multiplyByN)+(15.994915*multiplyByO)+(1.00794*multiplyByH)) == isotope_masses[i]-.5

与你在那里的第二个条件相同。

答案 1 :(得分:0)

if isotope_masses[i]+1 or isotope_masses[i]+1.1 or isotope_masses[i]+1.2 or isotope_masses[i]+.9 in mass_old2:

相当于:

if a or b or c in x:

正在评估为

if (bool(a) or bool(b) or bool(c)) in x:

您需要将其更改为以下格式:

if a in x or b in x or c in x:

非常确定这是How do I test one variable against multiple values?

的副本