[英]All outputs from function are the same
I have a function to read VCF files我有一个 function 来读取 VCF 文件
def readVCF(VCF):
VCF = VCF.readlines()
header = []
data = []
for line in VCF:
if line.startswith('#'):
header.append(line)
else:
data.append(line)
VCF_contents =[]
for line in data:
VCF_contents.extend(line.strip().split('\t'))
for i in range(0, len(VCF_contents), 10):
lines.append(VCF_contents[i : i+10])
The function works as I want it to, but problems start when I use it more than once. function 可以按我的意愿工作,但是当我多次使用它时就会出现问题。
I want to use this function multiple times, on different files, and the use the outputs from all these different files simultaneously.我想在不同的文件上多次使用这个 function,并同时使用所有这些不同文件的输出。 I have been using the function like this:
我一直在使用这样的 function:
VCF = open("frogs.txt", 'r')
lines = []
readVCF(VCF)
frogs = lines
VCF.close
VCF = open("rabbits.txt.", 'r')
readVCF(VCF)
rabbits = lines
VCF.close
VCF = open("lizards.txt", 'r')
readVCF(VCF)
lizards = lines
VCF.close
The problem is that, all my outputs are the same, and all have the output that comes from frogs.txt
, the first file I used the function for.问题是,我所有的输出都是相同的,并且都有来自
frogs.txt
的 output ,我使用 function 的第一个文件。 How can reuse this function without getting the same values?如何重用这个 function 而不会得到相同的值?
Possibly, you want to add:可能,您要添加:
lines = []
inside your function at the beginning and:在你的 function 的开头和:
return lines
at the end of your function, and call it like eg:在您的 function 的末尾,并调用它,例如:
VCF = open("frogs.txt", 'r')
frogs = readVCF(VCF)
VCF.close()
or alternatively (probably better):或者(可能更好):
with open("frogs.txt", 'r') as VCF:
frogs = readVCF(VCF)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.