python中的List,str和int帮助

时间:2012-07-06 17:57:31

标签: python elementtree

我有这样的号码列表:

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'对象不可订阅

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

1 个答案:

答案 0 :(得分: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)。最后的逗号是不必要的,startend现在是整数,因此请使用%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。