所以我正在使用写这个方法,它在查找数字方面工作正常,但它只返回最后一个值。是否有一种方法可以使它在每次运行后返回所有值。 这是我的代码:
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
答案 0 :(得分: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)
答案 1 :(得分:0)
您可以创建一个开始和结束元组的列表,只需在函数末尾返回列表。
答案 2 :(得分: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