0

我有这样的号码列表:

146, 168

174, 196

230, 252

258, 280

286, 308

314, 336

342, 364

370, 392

第一个数字代表我从我的代码中获得的值(起始数字),逗号后面的第二个数字是结束值。我尝试做的是同时使用开始值和结束值来打印字符串。这是我的代码的一部分:

root = etree.parse(f)

for lcn in root.xpath("/protein/match[@dbname='DB']/lcn"):
    start = lcn.get("start")
    end = lcn.get("end")
    print "%s, %s" % (start, end,)
    if start <= end:
        start = int(start+1)
        print start    
    if start <= end:

      print list(start)

      start = int(start+1)

我收到错误消息说我无法连接“str”和“int”对象。旁注:在列表索引中有一个字母。所以我的目标是在每个开始值和结束值的一行上打印出这些字母。例如 ACTGAGCAG 并可能导入到另一个输出文件。你能帮我解决这个问题吗?

更新:所以一切都解决了,我得到了结果,但现在我想让它们出现在一行上。我这样做了,但我收到错误消息说 TypeError: 'builtin_function_or_method' object is not subscriptable

    while start <= end:
        inRange = makeList.append[start]
        start += 1
        print inRange
4

1 回答 1

4

代替

start = lcn.get("start")
end = lcn.get("end")

利用

start = int(lcn.get("start"))
end = int(lcn.get("end"))

这是因为lcn.get返回一个字符串。

而不是start = int(start+1),使用start += 1. 您不再需要转换为整数并且start += 1start = start + 1.

而不是print "%s, %s" % (start, end,),使用print "%d, %d" % (start, end). 最后的逗号是不必要的,start并且end现在是整数,所以使用%d而不是%s.

更新:

而不是

while start <= end:
    inRange = makeList.append[start]
    start += 1
    print inRange

利用

for i in range(start, end):
    makeList.append(i)
    print(i)

如果使用 Python 3 或使用

for i in xrange(start, end):
    makeList.append(i)
    print i

如果使用 Python 2。

于 2012-07-06T17:59:00.610 回答