繁体   English   中英

在python中列出,str和int帮助

[英]List, str and int help in python

我有这样的号码清单:

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

代替

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。

暂无
暂无

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

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