Python if语句未考虑列表的最后一个元素
我是python的绝对初学者,我需要编写一段代码来区分两个列表。总体代码仍然有效,始终没有考虑列表的最后一个元素,并且“AT,AC”等列表被认为是相同的。我想得到一些帮助。谢谢Python if语句未考虑列表的最后一个元素,python,Python,我是python的绝对初学者,我需要编写一段代码来区分两个列表。总体代码仍然有效,始终没有考虑列表的最后一个元素,并且“AT,AC”等列表被认为是相同的。我想得到一些帮助。谢谢 Seq1 = input( " Enter first sequence ") Seq2 = input(" Enter second sequence ") seq1 = list(Seq1) seq2 = list(Seq2) def compare_seq(seq1,se
Seq1 = input( " Enter first sequence ")
Seq2 = input(" Enter second sequence ")
seq1 = list(Seq1)
seq2 = list(Seq2)
def compare_seq(seq1,seq2):
if len(seq1) != len(seq2):
print(" The sequences differ by their length ")
exit()
else:
for i in range(len(seq1)) :
if seq1[i] == seq2[i]:
print(" The sequences are the same ")
exit()
else :
print(" Sequences differ by a/mulitple nucleotide ")
exit()
compare_seq(seq1,seq2)
过早退出循环是一个常见错误:
for i in range(len(seq1)) :
if seq1[i] == seq2[i]:
print(" The sequences might be the same ") # note "might"
# exit() # <- leave this one out
else:
print(" Sequences differ by a/mulitple nucleotide ")
exit()
print(" The sequences are the same ") # now you know
对于列表,您也可以只检查相等性:
if seq1 == seq2:
print(" The sequences are the same ")
elif len(seq1) != len(seq2):
print(" The sequences differ by their length ")
else:
print(" Sequences differ by a/mulitple nucleotide ")
您只检查第一个元素并退出
Seq1 = input( " Enter first sequence ")
Seq2 = input(" Enter second sequence ")
seq1 = list(Seq1)
seq2 = list(Seq2)
flag = False
def compare_seq(seq1,seq2):
if len(seq1) != len(seq2):
print(" The sequences differ by their length ")
exit()
else:
for i in range(len(seq1)) :
if seq1[i] == seq2[i]:
continue
else :
flag = True
break
if flag == False:
print(" The sequences are the same ")
else:
print(" Sequences differ by a/mulitple nucleotide ")
exit()
compare_seq(seq1,seq2)
上面的代码应该对您有所帮助。它检查整个列表,而不是仅仅检查,如果元素不匹配,则将标志更改为True,但有几个问题:
Seq1 = input( " Enter first sequence ")
Seq2 = input(" Enter second sequence ")
seq1 = list(Seq1)
seq2 = list(Seq2)
def compare_seq(seq1,seq2):
if len(seq1) != len(seq2):
print(" The sequences differ by their length ")
#exit() No need for this exit as it quits python and makes you have to reopen it everytime you run your function
else:
if seq1 == seq2: #the original loop structure was comparing everysingle item in the list individually, but was then exiting python before completion. So in reality only the first element of each sequence was being compared
print(" The sequences are the same ")
else :
print(" Sequences differ by a/mulitple nucleotide ")
compare_seq(seq1,seq2)
这应该可以做到。您只需检查两个列表的索引中是否有任何值不相等,否则
返回true
例如:
def compare_seq(seq1,seq2):
if len(seq1) != len(seq2):
print("sequences dont share the same length")
return false
for i in range(len(seq1)):
if seq1[i] != seq2[i]:
print("sequences are not the same")
return false
return true
你能给我们一个输入/输出的例子吗?用
seq1=“AT”
和seq2=“AC”
的例子手工浏览你的代码。函数什么时候退出?到那时为止,你对清单中的多少进行了比较?(提示:考虑使用更长的测试输入;是否有什么变化?)在匹配的情况下,你退出得太早了。您需要不断迭代。仅仅因为一个索引匹配,并不意味着所有索引都匹配。我想您应该使用类似于seq1=seq1.split(',')
的东西。请注意,使用str
参数调用list
将为您提供单个字符的列表,例如list('a,B,C')==['a',',','B',','C']
@Nathanl谢谢!事实上,在第二次if声明之后,我离开得太早了。(* ^ ω ^)
def compare_seq(seq1,seq2):
if len(seq1) != len(seq2):
print("sequences dont share the same length")
return false
for i in range(len(seq1)):
if seq1[i] != seq2[i]:
print("sequences are not the same")
return false
return true