簡體   English   中英

字符串切片錯誤?

[英]Error with String Slicing?

我正在開發一個相當簡單的程序,將給定的非編碼DNA鏈轉換為它的所有對應鏈(編碼,mRNA,tRNA和氨基酸鏈)。

我在嘗試切片字符串時遇到了IndexError

mRNA_parts = mRNA.split(' ')
print mRNA_parts,  # using for debugging purposes

for base in mRNA_parts:
    print base # again, for debugging
    partA = base[0]
    partB = base[1]
    partC = base[2]
    my_acid = (amino_acids[partA][partB][partC]) + ' '
    # 'amino_acids' is a 3D (thrice-nested) dictionary with the corresponding parts of mRNA to convert to the amino acid chain.
    # part of the nested dictionary: amino_acids = {'U': {'U': {'U': 'Phe'}}}. This is only one part (out of 64) of it.
    # Thus, if  the mRNA section was 'UUU', the amino acid would be 'Phe '.
    acid_strand += my_acid

這是我得到的錯誤:

['GAU', '']
GAU


Traceback (most recent call last):
  File "D:\my_stuff\Google Drive\documents\SCHOOL\Programming\Python\DNA to RNA Converter.py", line 83, in <module>
    main()
  File "D:\my_stuff\Google Drive\documents\SCHOOL\Programming\Python\DNA to RNA Converter.py", line 3, in main
    convert(non_coding)
  File "D:\my_stuff\Google Drive\documents\SCHOOL\Programming\Python\DNA to RNA Converter.py", line 58, in convert
    partA = base[0]
IndexError: string index out of range

如果base為“ GAU”,base base[0]怎么做?

根據打印的內容,“ GAU”不是字符串嗎? 它周圍沒有任何報價。

提前致謝!

它不是顯示GAU的錯誤,而是顯示空字符串'' ,您將其作為列表的第二個元素。 在循環開始時添加一個測試以忽略它。

暫無
暫無

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

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