Python 如何使用循环来比较序列?

Python 如何使用循环来比较序列?,python,Python,我试图比较两个序列,a和b,并编写代码打印出这些不匹配的位置。我知道下面的代码是不正确的,但为了这个原因,这是我能找到的最接近的代码。我觉得我对流动的概念有些误解。任何帮助都将不胜感激。谢谢 a="CATCGTCCT" b="CACCGACCG" mismatch_seq=[] for x in list(a): mismatch=[] if x!=x in list(b): mismatch+=x in list(b) mismatch_seq.

我试图比较两个序列,a和b,并编写代码打印出这些不匹配的位置。我知道下面的代码是不正确的,但为了这个原因,这是我能找到的最接近的代码。我觉得我对流动的概念有些误解。任何帮助都将不胜感激。谢谢

a="CATCGTCCT"
b="CACCGACCG"
mismatch_seq=[]
for x in list(a):
    mismatch=[]
    if x!=x in list(b):
        mismatch+=x in list(b)
        mismatch_seq.append(mismatch)
    else:
       pass
print mismatch_seq

编辑:此外,您将如何编写代码来告诉您,在索引方面,这种不匹配发生在何处?

一个过于简单的代码可能会使用这种方法

a = "CATCGTCCT"                                             # SET a
b = "CACCGACCG"                                             # SET b

aListOfMismatchPOSITIONs = []                               # SET aList

for charPTR in range( len( a ) ):                           # LOOP over len( a )
    if  a[charPTR] != b[charPTR]:                           # .NE.
        aListOfMismatchPOSITIONs.append( charPTR)           #     APPEND
    else:                                                   # ELSE
        continue                                            #     LOOP^

print "A sequence of non-matching positions = ", aListOfMismatchPOSITIONs

一个过于简单的代码可能会用到这个

a = "CATCGTCCT"                                             # SET a
b = "CACCGACCG"                                             # SET b

aListOfMismatchPOSITIONs = []                               # SET aList

for charPTR in range( len( a ) ):                           # LOOP over len( a )
    if  a[charPTR] != b[charPTR]:                           # .NE.
        aListOfMismatchPOSITIONs.append( charPTR)           #     APPEND
    else:                                                   # ELSE
        continue                                            #     LOOP^

print "A sequence of non-matching positions = ", aListOfMismatchPOSITIONs

一个过于简单的代码可能会用到这个

a = "CATCGTCCT"                                             # SET a
b = "CACCGACCG"                                             # SET b

aListOfMismatchPOSITIONs = []                               # SET aList

for charPTR in range( len( a ) ):                           # LOOP over len( a )
    if  a[charPTR] != b[charPTR]:                           # .NE.
        aListOfMismatchPOSITIONs.append( charPTR)           #     APPEND
    else:                                                   # ELSE
        continue                                            #     LOOP^

print "A sequence of non-matching positions = ", aListOfMismatchPOSITIONs

一个过于简单的代码可能会用到这个

a = "CATCGTCCT"                                             # SET a
b = "CACCGACCG"                                             # SET b

aListOfMismatchPOSITIONs = []                               # SET aList

for charPTR in range( len( a ) ):                           # LOOP over len( a )
    if  a[charPTR] != b[charPTR]:                           # .NE.
        aListOfMismatchPOSITIONs.append( charPTR)           #     APPEND
    else:                                                   # ELSE
        continue                                            #     LOOP^

print "A sequence of non-matching positions = ", aListOfMismatchPOSITIONs
只要做:

a="CATCGTCCT"
b="CACCGACCG"
mismatch_seq=[]
for x in range (0, len(a)):
    if a[x] !=  b[x]:
        mismatch_seq.append(x)
print mismatch_seq
只要做:

a="CATCGTCCT"
b="CACCGACCG"
mismatch_seq=[]
for x in range (0, len(a)):
    if a[x] !=  b[x]:
        mismatch_seq.append(x)
print mismatch_seq
只要做:

a="CATCGTCCT"
b="CACCGACCG"
mismatch_seq=[]
for x in range (0, len(a)):
    if a[x] !=  b[x]:
        mismatch_seq.append(x)
print mismatch_seq
只要做:

a="CATCGTCCT"
b="CACCGACCG"
mismatch_seq=[]
for x in range (0, len(a)):
    if a[x] !=  b[x]:
        mismatch_seq.append(x)
print mismatch_seq

受前面答案的启发,本文研究
b
中存在
a
中相应字符的字符,如果这些位置不相等,则返回它们的索引
i

a = "CATCGTCCT"
b = "CACCGACCG"

mismatch = [i for i in range(0, len(a)) if a[i] != b[i]]

for pos in mismatch:
    print "At position", pos, "a had", a[pos], "while b had", b[pos], "."
不匹配的等效循环代码为:


受前面答案的启发,本文研究
b
中存在
a
中相应字符的字符,如果这些位置不相等,则返回它们的索引
i

a = "CATCGTCCT"
b = "CACCGACCG"

mismatch = [i for i in range(0, len(a)) if a[i] != b[i]]

for pos in mismatch:
    print "At position", pos, "a had", a[pos], "while b had", b[pos], "."
不匹配的等效循环代码为:


受前面答案的启发,本文研究
b
中存在
a
中相应字符的字符,如果这些位置不相等,则返回它们的索引
i

a = "CATCGTCCT"
b = "CACCGACCG"

mismatch = [i for i in range(0, len(a)) if a[i] != b[i]]

for pos in mismatch:
    print "At position", pos, "a had", a[pos], "while b had", b[pos], "."
不匹配的等效循环代码为:


受前面答案的启发,本文研究
b
中存在
a
中相应字符的字符,如果这些位置不相等,则返回它们的索引
i

a = "CATCGTCCT"
b = "CACCGACCG"

mismatch = [i for i in range(0, len(a)) if a[i] != b[i]]

for pos in mismatch:
    print "At position", pos, "a had", a[pos], "while b had", b[pos], "."
不匹配的等效循环代码为:


您可以只使用以下选项:

>>> filter(lambda m: m[1][0] != m[1][1] , enumerate(zip(a, b)))
[(2, ('T', 'C')), (5, ('T', 'A')), (8, ('T', 'G'))]

我建议使用
izip
。通过这种方式,您可以放入任意长度的整个文件,然后可以安全地将输出发送到文件…

您可以使用以下方法:

>>> filter(lambda m: m[1][0] != m[1][1] , enumerate(zip(a, b)))
[(2, ('T', 'C')), (5, ('T', 'A')), (8, ('T', 'G'))]

我建议使用
izip
。通过这种方式,您可以放入任意长度的整个文件,然后可以安全地将输出发送到文件…

您可以使用以下方法:

>>> filter(lambda m: m[1][0] != m[1][1] , enumerate(zip(a, b)))
[(2, ('T', 'C')), (5, ('T', 'A')), (8, ('T', 'G'))]

我建议使用
izip
。通过这种方式,您可以放入任意长度的整个文件,然后可以安全地将输出发送到文件…

您可以使用以下方法:

>>> filter(lambda m: m[1][0] != m[1][1] , enumerate(zip(a, b)))
[(2, ('T', 'C')), (5, ('T', 'A')), (8, ('T', 'G'))]


我建议使用
izip
。这样,您可以放入任意长度的整个文件,然后可以安全地将输出发送到文件…

您不需要使用
list()
构造。字符串可以在Python中迭代。您不需要使用
list()
构造。字符串可以在Python中迭代。您不需要使用
list()
构造。字符串可以在Python中迭代。您不需要使用
list()
构造。字符串可以在Python中迭代。啊,好的,谢谢。你如何让代码告诉你不匹配元素的索引呢?只要用
mismatch\u-seq.append(x)
替换为
mismatch\u-seq.append(a[x])
mismatch\u-seq.append(b[x])
我仍然只得到元素,而不是元素的索引。当你说index时,你的意思是像弦中的位置,对吗
mismatch_eq.append(x)
应该可以获得不匹配字符的索引(字符串中的位置)。注意,它从0开始计数,而不是从1开始。例如,当我运行这个示例时,您应该得到[2,5,8],这是不匹配字符的索引。如果您希望代码同时显示索引和元素,只需创建一个新列表,例如
mismatch\u idx
,并在
mismatch\u seq.append(x)
下放置
mismatch\u idx.append(a[x]
mismatch\u idx.append(b[x])
。啊,好的,谢谢。你会如何要求代码告诉你不匹配元素的索引?只需将
mismatch_seq.append(x)
替换为
mismatch_seq.append(a[x])
mismatch_seq.append(b[x])
我仍然只获取元素,而不是元素的索引。当你说index时,你的意思是像字符串中的位置一样,对吗?
不匹配\u eq.append(x)
应该获取索引(字符串中的位置)不匹配字符的索引。请注意,它从0开始计数,而不是从1开始计数。例如,当我运行此示例时,您应该得到[2,5,8],这是不匹配字符的索引。如果您希望代码同时显示索引和元素,只需创建一个新列表,例如
mismatch\u idx
mismatch\u seq.append(x) 
,put
mismatch\u idx.append(a[x]
mismatch\u idx.append(b[x])
。啊,好的,谢谢。你如何让代码告诉你不匹配元素的索引呢?只需将
mismatch\u seq.append(x)
替换为
mismatch\u seq.append(a[x])
mismatch\u seq.append(b[x])
我仍然只获取元素,而不是元素的索引。当你说index时,你的意思是像字符串中的位置一样,对吗?
不匹配\u eq.append(x)
应该获取索引(字符串中的位置)不匹配字符的索引。请注意,它从0开始计数,而不是从1开始计数。例如,当我运行此示例时,您应该得到[2,5,8],这是不匹配字符的索引。如果您希望代码同时显示索引和元素,只需创建一个新列表,例如
mismatch\u idx
mismatch\u seq.append(x) 
,put
mismatch\u idx.append(a[x]
mismatch\u idx.append(b[x])
。啊,好的,谢谢。你怎么让代码告诉你不匹配元素的索引呢?只要用
mismatch\u seq.append(x)
替换为
mismatch\u seq.appe即可