简体   繁体   English

function 的所有输出都相同

[英]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.

相关问题 从一个函数返回所有可能的输出,然后在另一个函数中使用它 - return all possible outputs from a function then use it in another function 如何从 keras 中的单个自定义损失 function 访问所有输出 - How to access all outputs from a single custom loss function in keras How to import a function from an R package as if it was native Python function and use all its outputs? - How to import a function from an R package as if it was native Python function and use all its outputs? 从函数返回多个输出 - Returning multiple outputs from function 在循环中运行相同的 function 并保存输出 - Running the same function in loop and saving the outputs Pytorch BCELoss function 不同输出对应相同输入 - Pytorch BCELoss function different outputs for same inputs 是否有 function 为相同的 X 输出 Y 的最大值? - Is there a function that outputs a max value of Y for the same Xs? 在添加模函数后,所有输出均为零 - After adding a modulo to function, all outputs are zero 如何将“INVALID”分配给 function 的所有输出? - How to assign “INVALID” to all outputs of function? Theano神经网络所有输入的所有输出收敛到相同的值 - Theano Neural Net All Outputs Converge to Same Value For All Inputs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM