第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

if 語句不考慮列表的最后一個(gè)元素

if 語句不考慮列表的最后一個(gè)元素

LEATH 2023-12-26 14:52:02
我是 python 的絕對(duì)初學(xué)者,我需要編寫一個(gè)可以區(qū)分兩個(gè)列表的代碼。總體代碼仍然有效,始終不考慮列表的最后一個(gè)元素,并且諸如“AT,AC”之類的列表被認(rèn)為是相同的。我希望得到一些幫助。謝謝 !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貢獻(xiàn)1155條經(jīng)驗(yàn) 獲得超0個(gè)贊

過早退出循環(huán)是一個(gè)常見的錯(cuò)誤:


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

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


# ...

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

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


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


查看完整回答
反對(duì) 回復(fù) 2023-12-26
?
瀟湘沐

TA貢獻(xiàn)1816條經(jīng)驗(yàn) 獲得超6個(gè)贊

差一點(diǎn)就解決了,但是有幾個(gè)問題:


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)


這應(yīng)該可以解決問題。


查看完整回答
反對(duì) 回復(fù) 2023-12-26
?
慕運(yùn)維8079593

TA貢獻(xiàn)1876條經(jīng)驗(yàn) 獲得超5個(gè)贊

您只檢查第一個(gè)元素并退出。


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)


上面的代碼應(yīng)該對(duì)你有幫助。它檢查整個(gè)列表,而不是僅僅檢查,如果元素不匹配,則將標(biāo)志更改為 True


查看完整回答
反對(duì) 回復(fù) 2023-12-26
?
MM們

TA貢獻(xiàn)1886條經(jīng)驗(yàn) 獲得超2個(gè)贊

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


查看完整回答
反對(duì) 回復(fù) 2023-12-26
  • 4 回答
  • 0 關(guān)注
  • 193 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)