【问题标题】:Biopython for Loop IndexErrorBiopython for Loop IndexError
【发布时间】:2016-03-09 21:21:34
【问题描述】:

当我输入此代码时,我得到“IndexError: list is out of range”。此外,retmax 设置为 614,因为这是我提出请求时的结果总数。有没有办法使用根据搜索结果而变化的变量使 retmode 等于结果数?

#!/usr/bin/env python

from Bio import Entrez
Entrez.email = "something@gmail.com"
handle1 = Entrez.esearch(db = "nucleotide", term = "dengue full genome", retmax = 614) 
record = Entrez.read(handle1)
IdNums = [int(i) for i in record['IdList']]

while i >= 0 and i <= len(IdNums):
handle2 = Entrez.esearch(db = "nucleotide", id = IdNums[i], type = "gb", retmode = "text")
record = Entrez.read(handle2)
print(record)
i += 1

【问题讨论】:

  • 我打错了:handle2的定义有一个and括号。
  • 请更新您的代码以反映您所做的更改 -- 然后我们可以从那里开始。
  • 好的,我刚刚修正了错字。但我仍然遇到 for 循环与 IdNums[i] 交互的越界错误问题。

标签: loops for-loop biopython


【解决方案1】:

您可以使用 for 循环,而不是使用 while 循环...

from Bio import Entrez
Entrez.email = 'youremailaddress'
handle1 = Entrez.esearch(db = 'nucleotide', term = 'dengue full genome', retmax = 614)
record = Entrez.read(handle1)
IdNums = [int(i) for i in record['IdList']]

for i in IdNums:
   print(i)
   handle2 = Entrez.esearch(db = 'nucleotide', term = 'dengue full genome', id = i, rettype = 'gb', retmode = 'text')
   record = Entrez.read(handle2)
   print(record)

我在我的电脑上运行它,它似乎工作。 for 循环解决了越界问题,handle2 中添加项解决了调用错误。

【讨论】:

  • 感谢您的帮助!
  • 很高兴为您提供帮助。 如果代码回答了您的问题 - 请考虑接受该解决方案。
猜你喜欢
  • 2020-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多