简体   繁体   English

字符串切片错误?

[英]Error with String Slicing?

I am working on a fairly simple program to convert a given non-coding strand of DNA to all of it's counterparts (coding, mRNA, tRNA, and amino acid chain). 我正在开发一个相当简单的程序,将给定的非编码DNA链转换为它的所有对应链(编码,mRNA,tRNA和氨基酸链)。

I get an IndexError when I try to slice a string though: 我在尝试切片字符串时遇到了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

This is the error I get: 这是我得到的错误:

['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

How is it unable to do base[0] where base is 'GAU'? 如果base为“ GAU”,base base[0]怎么做?

Based off of what it printed, is 'GAU' not a string? 根据打印的内容,“ GAU”不是字符串吗? It does not have any quotations around it. 它周围没有任何报价。

Thanks in advance! 提前致谢!

It's not showing the error for GAU , but for the empty string '' , that you are having as the second element of your list. 它不是显示GAU的错误,而是显示空字符串'' ,您将其作为列表的第二个元素。 Add a test at the start of your loop to ignore that. 在循环开始时添加一个测试以忽略它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM