簡體   English   中英

列表索引超出范圍但不是(我不認為)

[英]List index out of range but it's not (i dont think)

我正在編寫一個程序來將一串 RNA 破譯成蛋白質,但我遇到了一個錯誤。 它在第 86 行說,列表索引超出范圍。 我認為這是不對的,因為該列表比它正在測試的列表項要長得多。 這是我的代碼:

 #Find start protein
  proteins = []
  i = 0
  a = 0
  for i in range(len(dna) // 3):
    current = rna_split[a]
    if current[0] == "A":
      if current[1] == "U":
        if current[2] == "G":
          proteins.append("Met")
          break
    proteins.append('   ')
    a = a + 1
    i = i + 1
    print(a)

  #See if no start codon
  if a == (len(dna) + 1):
    print("No Start Codon")
    raise SystemExit(0)

  #Find Proteins
  a = a + 1
  for i in range(len(dna) // 3):
    current = rna_split[a]
    if current[0] == "U":
      if current[1] == "A":
        if current[2] == "A" or current[2] == "G":
          proteins.append("Stop")
          break
        else:
          proteins.append("Tyr")
      elif current[1] == "G":
        if current[2] == "A":
          proteins.append("Stop")
          break
        elif current[2] == "G":
          proteins.append("Try")
        else:
          proteins.append("Cys")
      elif current[1] == "C":
        proteins.append("Ser")
      elif current[1] == "U":
        if current[2] == "A" or current[2] == "G":
          proteins.append("Leu")
        else:
          proteins.append("Phe")
    elif current[0] == "C":
      if current[1] == "G":
        proteins.append("Arg")
      elif current[1] == "U":
        proteins.append("Leu")
      elif current[1] == "C":
        proteins.append("Pro")
      elif current[1] == "A":
        if current[2] == "U" or current[2] == "C":
          proteins.append("His")
        else:
          proteins.append("Glu")
    elif current[0] == "A":
      if current[1] == "U":
        if current[2] == "A" or current[2] == "C" or current[2] == "U":
          proteins.append("Iso")
      elif current[2] == "C":
        proteins.append("Thr")
      elif current[1] == "A":
        if current[2] == "C" or current[2] == "U":
          proteins.append("Asp")
        else:
          proteins.append("Lys")
      elif current[1] == "G":
        if current[2] == "C" or current[2] == "U":
          proteins.append("Ser")
        else:
          proteins.append("Arg")
    else:
      if current[1] == "U":
        proteins.append("Val")
      elif current[1] == "C":
        proteins.append("Ala")
      elif current[1] == "G":
        proteins.append("Gly")
      else:
        if current[2] == "U" or current[2] == "C":
          proteins.append("Asp Acid")
        else:
          proteins.append("Glu Acid")
    a = a + 1
    i = i + 1

要知道的事情:

  • 變量 dna 總是能被 3 整除
  • 查找蛋白質部分應該從列表項 1 開始搜索,而不是找到起始蛋白質的位置

我試圖通過在某些點打印東西並將 a 設置為不同的值來調試它,但沒有任何效果。 到目前為止,我已經嘗試了一兩個小時,但我無法弄清楚出了什么問題。

看起來問題是您在代碼示例的兩個for循環中都使用了變量a ,但在第一個循環之后不要重置它的值。 除了使用a ,您可以只使用i ,它已定義但從未使用過。 也不需要手動增加i變量,循環會為你處理。

或者,一種更 Pythonic 的方法是迭代rna_split的元素(使用for x in rna_split: ),而不是使用索引進行迭代。

例如:

for rna_triplet in rna_split:
    if rna_triplet[0] == "A":
        # ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM