亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

if 語句不考慮列表的最后一個元素

if 語句不考慮列表的最后一個元素

LEATH 2023-12-26 14:52:02
我是 python 的絕對初學者,我需要編寫一個可以區分兩個列表的代碼??傮w代碼仍然有效,始終不考慮列表的最后一個元素,并且諸如“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)
查看完整描述

4 回答

?
白衣非少年

TA貢獻1155條經驗 獲得超0個贊

過早退出循環是一個常見的錯誤:


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

此模式有一個內置的快捷方式 ( all),您可以將其結合起來zip使之更加簡潔:


# ...

else:    

    if all(x == y for x, y in zip(seq1, seq2)):

        print(" The sequences are the same ")

    else:

        print(" Sequences differ by a/mulitple nucleotide ")

對于列表,您也可以只檢查相等性:


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 ")


查看完整回答
反對 回復 2023-12-26
?
瀟湘沐

TA貢獻1816條經驗 獲得超6個贊

差一點就解決了,但是有幾個問題:


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)


這應該可以解決問題。


查看完整回答
反對 回復 2023-12-26
?
慕運維8079593

TA貢獻1876條經驗 獲得超5個贊

您只檢查第一個元素并退出。


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


查看完整回答
反對 回復 2023-12-26
?
MM們

TA貢獻1886條經驗 獲得超2個贊

您可以只檢查兩個列表的索引處的值是否不相等,否則return 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


查看完整回答
反對 回復 2023-12-26
  • 4 回答
  • 0 關注
  • 202 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號