Python:返回True如果... else False语句(布尔值)

时间:2017-10-30 19:18:48

标签: python

  

A< - >之间的转换。 G和C - < - > Ť
  A< - >之间的转换。 C和G - < - > Ť

     

作业:

     
      
  • 写一个需要两个核苷酸的函数转换。该函数必须返回一个布尔值,该值指示第二个核苷酸替换第一个核苷酸是否会导致转换。
  •   

问题:我认为该函数不承认or背后的陈述。 代码在某些情况下不起作用,例如:transition('A', 'G')为True,我的代码为False

  
      
  • 写一个功能比率,它取两个DNA序列s1和s2
      该函数可以假设两个序列具有相同的长度(该函数不需要明确地检查它)。该函数必须将两个给定序列的转换/转换比R(s 1,s 2)∈R作为浮点数返回。如果两个序列之间没有颠换,根据定义R(s 1,s 2)= 0。
  •   

问题:代码无效

def transition(letter1, letter2):        
    """
    >>> transition('G', 'A')
    True
    >>> transition('t', 'g')
    False
    >>> transition('C', 'c')
    False
    """    
    return True if letter1.lower() == 'gt' and letter2.lower() == 'ac' or letter1.lower() == 'ac' and letter2.lower() == 'gt' else False


def transversion(letter1, letter2):
    """
    >>> transversion('G', 'A')
    False
    >>> transversion('t', 'g')
    True
    >>> transversion('C', 'c')
    False
    """
    return True if letter1.lower() == 'ct' and letter2.lower() == 'ag' or  letter1.lower() == 'ag' and letter2.lower() == 'ct' else False


def ratio(seq1, seq2):
    """
    >>> ratio('ATTAGCATTATCATC', 'AAATAGGATATATGG')
    0.2222222222222222
    >>> seq1 = 'GCAACGCACAACGAAAACCCTTAGGGACTGGATTATTTCGTGATCGTTGTAGTTATTGGAAGTACGGGCATCAACCCAGTT'
    >>> seq2 = 'ttatctgacaaagaaagccgtcaacggctggataatttcgcgatcgtgctggttactggcggtacgagtgttcctttgggt'
    >>> ratio(seq1, seq2)   
    1.2142857142857142
    """
    count = 0
    tel = 0

    for i in range(len(seq1)):
        if transition(seq1[i], seq2[i]):
            count += 1

    for i in range(len(seq1)):
        if transversion(seq1[i], seq2[i]):
            tel += 1

    if tel != 0:
        return float(count / tel)
    else:
        return 0 

if __name__ == '__main__':
    import doctest
    doctest.testmod()

4 个答案:

答案 0 :(得分:1)

更改这样的行

return True if letter1.lower() == 'gt' and letter2.lower() == 'ac' or letter1.lower() == 'ac' and letter2.lower() == 'gt' else False

by:

return (letter1.lower() == 'gt' and letter2.lower() == 'ac' or letter1.lower() == 'ac' and letter2.lower() == 'gt') 

答案 1 :(得分:1)

我认为相同核碱基的transitiontransversion必须返回False
(即transition('A','A') == False)

您可以使用简单的命名谓词来简化:https://repl.it/N4TC/4

def transition(nucleobase1, nucleobase2):        
   """ True if both are different and are purine
   """
   return (not isEqual(nucleobase1, nucleobase2) and
           isPurine(nucleobase1) and 
           isPurine(nucleobase2))

def transversion(nucleobase1, nucleobase2):        
   """ True if both are different and not transition
   """
   return (not isEqual(nucleobase1, nucleobase2) and
           not transition(nucleobase1, nucleobase2))

其他谓词:

### nucleobase Predicat    

def isAdenine(nucleobase):
    """ True if adenine (A)
    """
    return nucleobase.lower()=='a'

def isCytosine(nucleobase):
    """ True if cytosine (C)
    """
    return nucleobase.lower()=='c'

def isGuanine(nucleobase):
    """ True if guanine (G)
    """
    return nucleobase.lower()=='g'

def isThymine(nucleobase):
    """ True if thymine (T)    
    """
    return nucleobase.lower()=='t'

def isPurine(nucleobase):
    """ True if adenine (A) or guanine (G)
    """
    return isAdenine(nucleobase) or isGuanine(nucleobase)

def isPyrimidine(nucleobase):
    """ True if cytosine (C) or thymine (T)
    """
    return isCytosine(nucleobase) or isThymine(nucleobase)

def isEqual(nucleobase1, nucleobase2):
    """ Equal ignore case
    """
    return nucleobase1.lower()==nucleobase2.lower()      

答案 2 :(得分:1)

作为一般经验法则,如果您有多个条件,则可以先将它们分配给变量。可能不是您正在寻找的答案,但可能可以帮助您编写更易于阅读的代码。

像这样:

letter1 = "GT"
letter2 = "AC"

def transition(letter1, letter2):

    cond1 = (letter1.lower() == 'gt')
    cond2 = (letter2.lower() == 'ac')
    cond3 = (letter1.lower() == 'ac')
    cond4 = (letter2.lower() == 'gt')

    if (cond1 and cond2) or (cond3 and cond4):
        return True
    else:
        return False

transition(letter1,letter2)

答案 3 :(得分:0)

试试这个:

def transition(letter1, letter2):        
   """
   >>> transition('G', 'A')
   True
   >>> transition('t', 'g')
   False
   >>> transition('C', 'c')
   False
   """
   if letter1.lower() == letter2.lower():
       return False

   return (letter1.lower() in 'ag' and letter2.lower() in 'ag') or (letter1.lower() in 'ct' and letter2.lower() in 'ct')

def transversion(letter1, letter2):        
   """
   >>> transversion('G', 'A')
   False
   >>> transition('t', 'g')
   True
   >>> transition('C', 'c')
   False
   """
   if letter1.lower() == letter2.lower():
       return False

   return (letter1.lower() in 'ac' and letter2.lower() in 'ac') or (letter1.lower() in 'gt' and letter2.lower() in 'gt')

似乎letter1.lower() == 'gt'你试图检查字母是g还是t。您可以使用in代替==