我正在尝试在两个相同长度的字符串之间找到匹配项,而我正在使用以下代码。
import operator
seq1 = input("Type the first sequence: ")
seq2 = input("Type the second sequence: ")
if len(seq1) != len(seq2):
print("Sequences must be of the same length")
else:
for i in range(len(seq1)):
if (seq1[i] == seq2[i]):
print ("There are", sum(map(operator.eq, seq1, seq2)), "matches:"+"\n", seq1+"\n", seq2+"\n", "*"*sum(map(operator.eq, seq1, seq2)))
问题是我想把“*”放在匹配的位置,我不知道如何表明这些位置。 另一个问题是,当我想打印结果时,每次匹配都会重复一次。我可以给你举个例子:
Type the first sequence: ATCGA
Type the second sequence: AACCA
There are 3 matches:
ATCGA
AACCA
***
There are 3 matches:
ATCGA
AACCA
***
There are 3 matches:
ATCGA
AACCA
***
我真正想要的是以下内容:
Type the first sequence: ATCGA
Type the second sequence: AACCA
There are 3 matches:
ATCGA
AACCA
* * *
答案 0 :(得分:0)
您可以使用生成器表达式并将其连接以返回字符串:
str1 = 'ATCGA'
str2 = 'AACCA'
if len(seq1) != len(seq2):
print("Sequences must be of the same length")
else:
matches = ''.join('*' if i==j else ' ' for i, j in zip(str1, str2))
print("There are %d matches" % matches.count("*"))
print(str1)
print(str2)
print(matches)
答案 1 :(得分:0)
你可以试试这个:
seq1 = input("Type the first sequence: ")
seq2 = input("Type the second sequence: ")
if len(seq1) != len(seq2):
print("Sequences must be of the same length")
else:
asterisk_string = "".join("*" if seq1[i] == seq2[i] else " " for i in range(len(seq1)))
print("%d matches" % asterisk_string.count("*"))
print(seq1)
print(seq2)
print(asterisk_string)
示例输出:
Type the first sequence: ATCGA
Type the second sequence: AACCA
3 matches
ATCGA
AACCA
* * *
答案 2 :(得分:0)
实际上你在这里混合了一些概念。你正在创建一个循环并在循环内部创建map
。你只需要一个。
假设您要查找匹配元素的位置:
[i for i in map(operator.eq, seq1, seq2)]
和
matches = [None] * len(seq1)
for i in range(len(seq1)):
if (seq1[i] == seq2[i]):
matches[i] = True
else:
matches[i] = False
完全等同。
所以要生成一个你想要的结果,你可以改变第二个结果:
seq1 = 'ATCGA'
seq2 = 'AACCA'
matches = [None] * len(seq1)
for i in range(len(seq1)):
if (seq1[i] == seq2[i]):
matches[i] = '*'
else:
matches[i] = ' '
或替代地图:
matches = ['*' if i else ' ' for i in map(operator.eq, seq1, seq2)]
然后将它们转换为输出:
print(seq1)
print(seq2)
print(''.join(matches))