首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >字符串切片出错吗?

字符串切片出错吗?
EN

Stack Overflow用户
提问于 2012-12-22 17:17:40
回答 1查看 117关注 0票数 0

我正在进行一个相当简单的程序,将一个给定的非编码链DNA转换为所有它的对应链(编码、mRNA、tRNA和氨基酸链)。

但是,当我试图分割字符串时,我会得到一个IndexError

代码语言:javascript
复制
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

这是我得到的错误:

代码语言:javascript
复制
['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[0]的基地是'GAU'?

根据它打印的内容,“GAU”不是字符串吗?它周围没有任何报价。

提前感谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-12-22 17:19:16

它显示的不是GAU的错误,而是作为列表第二个元素的空字符串''的错误。在循环开始时添加一个测试来忽略这一点。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14004880

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档