3

所以我正在使用编写这个方法,它在查找数字方面工作正常,但它只返回最后一个值。有没有办法让它在每次运行后返回所有值。这是我的代码:

def searchPFAM():

    fileAddress = '/Volumes/interpro/data/Q14591.txt'
    start = None
    end = None
    with open(fileAddress,'rb') as f:
        root = etree.parse(f)
        for lcn in root.xpath("/protein/match[@dbname='PFAM']/lcn"):#find dbname =PFAM
            start = int(lcn.get("start"))#if it is PFAM then look for start value
            end = int(lcn.get("end"))#if it is PFAM then also look for end value
            print start, end
        return start, end
4

3 回答 3

5

你的意思是类似的吗?

def do_something(fname):
    with open(fname,'rb') as f:
        root = etree.parse(f)
        for lcn in root.xpath("/protein/match[@dbname='PFAM']/lcn"):#find dbname =PFAM
            # Make slightly more robust
            try:
                start = int(lcn.get("start"))#if it is PFAM then look for start value
                end = int(lcn.get("end"))#if it is PFAM then also look for end value
                yield start, end
            except (TypeError , ValueError) as e:
                pass # start/end aren't usable as numbers decide what to do here...

for start, end in do_something():
    do_something_else(start, end)
于 2012-07-12T17:24:39.733 回答
0

您可以创建一个包含 start 和 end 的元组列表,并在函数末尾返回该列表。

于 2012-07-12T17:24:45.870 回答
0

只需修改您的函数以创建并返回开始、结束元组的列表:

def searchPFAM():
    fileAddress = '/Volumes/interpro/data/Q14591.txt'
    start = None
    end = None
    result = []
    with open(fileAddress,'rb') as f:
        root = etree.parse(f)
        for lcn in root.xpath("/protein/match[@dbname='PFAM']/lcn"):#find dbname =PFAM
            start = int(lcn.get("start"))#if it is PFAM then look for start value
            end = int(lcn.get("end"))#if it is PFAM then also look for end value
            print start, end
        result.append((start, end))
    return result

可读性稍差,但更紧凑和更有效的编写方式是使用所谓的“列表理解”,如下所示:

def searchPFAM():
    fileAddress = '/Volumes/interpro/data/Q14591.txt'
    start = None
    end = None
    with open(fileAddress,'rb') as f:
        root = etree.parse(f)
        result = [(int(lcn.get("start")), int(lcn.get("end"))) 
                     for lcn in root.xpath("/protein/match[@dbname='PFAM']/lcn")]
    return result

之后您可以像这样处理返回的列表:

for start,end in result:
   ... # do something with pair of int values

或者

for i in xrange(len(result)):
   start,end = result[i][0],result[i][1]
   ... # do something with pair of int values
于 2012-07-12T17:30:46.040 回答