if 语句不考虑列表的最后一个元素

我是 python 的绝对初学者,我需要编写一个可以区分两个列表的代码。总体代码仍然有效,始终不考虑列表的最后一个元素,并且诸如“AT,AC”之类的列表被认为是相同的。我希望得到一些帮助。谢谢 !


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)


LEATH
浏览 55回答 4
4回答

白衣非少年

过早退出循环是一个常见的错误:for i in range(len(seq1)) :&nbsp; &nbsp; if seq1[i] == seq2[i]:&nbsp; &nbsp; &nbsp; &nbsp; print(" The sequences might be the same ")&nbsp; # note "might"&nbsp; &nbsp; &nbsp; &nbsp; # exit()&nbsp; # <- leave this one out&nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; print(" Sequences differ by a/mulitple nucleotide ")&nbsp; &nbsp; &nbsp; &nbsp; exit()print(" The sequences are the same ")&nbsp; # now you know此模式有一个内置的快捷方式 ( all),您可以将其结合起来zip使之更加简洁:# ...else:&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; if all(x == y for x, y in zip(seq1, seq2)):&nbsp; &nbsp; &nbsp; &nbsp; print(" The sequences are the same ")&nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; print(" Sequences differ by a/mulitple nucleotide ")对于列表,您也可以只检查相等性:if seq1 == seq2:&nbsp; &nbsp; print(" The sequences are the same ")elif len(seq1) != len(seq2):&nbsp; &nbsp; print(" The sequences differ by their length ")else:&nbsp; &nbsp; print(" Sequences differ by a/mulitple nucleotide ")

潇湘沐

差一点就解决了,但是有几个问题:Seq1 = input( " Enter first sequence ")Seq2 = input(" Enter second sequence ")seq1 = list(Seq1)seq2 = list(Seq2)def compare_seq(seq1,seq2):&nbsp;if len(seq1) != len(seq2):&nbsp; print(" The sequences differ by their length ")&nbsp; #exit()&nbsp; No need for this exit as it quits python and makes you have to reopen it everytime you run your function&nbsp;else:&nbsp; &nbsp;if seq1 == seq2:&nbsp; #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&nbsp; &nbsp; print(" The sequences are the same ")&nbsp; &nbsp;else :&nbsp; &nbsp; print(" Sequences differ by a/mulitple nucleotide ")compare_seq(seq1,seq2)这应该可以解决问题。

慕运维8079593

您只检查第一个元素并退出。Seq1 = input( " Enter first sequence ")Seq2 = input(" Enter second sequence ")seq1 = list(Seq1)seq2 = list(Seq2)flag = Falsedef compare_seq(seq1,seq2):&nbsp;if len(seq1) != len(seq2):&nbsp; print(" The sequences differ by their length ")&nbsp; exit()&nbsp;else:&nbsp; for i in range(len(seq1)) :&nbsp; &nbsp;if seq1[i] == seq2[i]:&nbsp; &nbsp; continue&nbsp; &nbsp;else :&nbsp; &nbsp; flag = True&nbsp; &nbsp; break&nbsp;&nbsp;if flag == False:&nbsp; print(" The sequences are the same ")&nbsp;else:&nbsp; print(" Sequences differ by a/mulitple nucleotide ")&nbsp;exit()compare_seq(seq1,seq2)上面的代码应该对你有帮助。它检查整个列表,而不是仅仅检查,如果元素不匹配,则将标志更改为 True

MM们

您可以只检查两个列表的索引处的值是否不相等,否则return true。例子:def compare_seq(seq1,seq2):&nbsp;if len(seq1) != len(seq2):&nbsp; &nbsp; &nbsp;print("sequences dont share the same length")&nbsp; &nbsp; &nbsp;return false&nbsp;for i in range(len(seq1)):&nbsp; &nbsp; &nbsp;if seq1[i] != seq2[i]:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;print("sequences are not the same")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return false&nbsp;&nbsp;return true
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python