【问题标题】:Comparing DNA sequences in Python 3在 Python 3 中比较 DNA 序列
【发布时间】:2015-03-30 08:28:41
【问题描述】:

到目前为止我的程序是:

seq_a = "TGGAGGCAATGGCGGCCAGCA"
seq_b = "GACTCCTCCTCCTCCTGCTCA"    
len_a = len(seq_a)    
len_b = len(seq_b)    
print("Length of Sequence A: " + str(len_a))    
print()    
print("Length of Sequence B: " + str(len_b))
print()

def sequence_compare(seq_a, seq_b):
        len1 = len(seq_a)
        len2 = len(seq_b)
        mismatches = []
        for pos in range (0, min(len1, len2)) :
              if seq_a[pos] != seq_b[pos]:
                  mismatches.append('|')
              else:
                  mismatches.append(' ')
        print (seq_a)
        print (mismatches)
        print (seq_b)
sequence_compare(seq_a,seq_b)

我希望输出如下:

TGGAGGCAATGGCGGCCAGCA
||||||||| |||||||||
GACTCCTCCTCCTCCTGCTCA

但是输出却是:

TGGAGGCAATGGCGGCCAGCA
['|', '|', '|', '|', '|', '|', '|', '|', '|', ' ', '|', '|', '|', '|', '|', '|', '|', '|', '|', ' ', ' ']
GACTCCTCCTCCTCCTGCTCA

【问题讨论】:

    标签: python string list python-3.x


    【解决方案1】:

    你已经很近了..

    print ("".join(mismatches))
    

    会做的伎俩并根据需要打印

    ||||||||| |||||||||
    

    【讨论】:

    • 感谢工作出色的人!不知道为什么,仍在学习 Python 3
    • 它所做的只是说,“按顺序获取不匹配字符列表,并将它们连接成一个字符串,没有任何分隔。”如果你完成了",".join(mismatches),你会得到相同的输出,用逗号分隔而不是挤在一起。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-10
    • 1970-01-01
    • 1970-01-01
    • 2019-04-16
    • 1970-01-01
    • 2012-06-14
    相关资源
    最近更新 更多