简体   繁体   English

如何在python中的另一个函数中使用一个函数中的数据?

[英]How do I use data from one function in another function in python?

Hello I am pretty new to python and I want to do the following:你好,我对 python 很陌生,我想做以下事情:

I have a function that opens a file, reads the file, closes the file and returns the data:我有一个打开文件、读取文件、关闭文件并返回数据的函数:

def getFastaFromFile(filename):
    """ Read a fasta file (filename) from disk and return
    its full contents as a string"""
    inf=open(filename)
    data=inf.read()
    inf.close()
    return data

The data that is being returned are a few lines with strings.返回的数据是带有字符串的几行。

What I want to do is have another function that uses the data from the first function and perform the .readlines() , .readline() and .count() commands我想要做的是使用第一个函数中的数据并执行.readlines().readline().count()命令的另一个函数

My second function:我的第二个功能:

def printTableFromFasta(fastarec):
    a= data.readlines()
    for i in range(a)
        b= data.readline()
        c= b.count('A')
        print(c)

As output I would like to print the amount of times string "A" appears for every line from the data.作为输出,我想打印数据中每一行出现字符串“A”的次数。 The problem I get with this code is that the data doesn't get recognized.我使用此代码遇到的问题是无法识别数据。

First, you need to pass the data you are wanting to read into the second function, like so首先,您需要将要读取的数据传递到第二个函数中,如下所示

def printTableFromFasta(data):

In order to get this from your first function, try returning the entire contents of the file为了从你的第一个函数中得到这个,尝试返回文件的全部内容

def getFastaFromFile(filename):
    with open(filename, 'r') as inf: # handles open and close
        data = inf.readlines()       # Returns the entire file as a list of strings
    return data

Your function call will look something like this你的函数调用看起来像这样

printTableFromFasta(getFastaFromFile(filename))

Then, in your second function, you don't need to call readlines , it's already a list.然后,在你的第二个函数中,你不需要调用readlines ,它已经是一个列表。

def printTableFromFasta(data):
    for line in data            # look at each line
        print(line.count('A'))  # count 'A'

Edit: To only read from the second function and not touch the first function编辑:只从第二个功能中读取而不是触摸第一个功能

def printTableFromFasta(filename):
    with open(filename, 'r') as inf: # handles open and close
        for line in inf.readlines()  # look at each line
            print(line.count('A'))   # count 'A'

Remember that the data variable in the first function is local .请记住,第一个函数中的data变量是local It cannot be accessed from outside the function it is defined in.不能从定义它的函数外部访问它。

For example, the getName() function returns a variable which is locally called data but you access the value by calling the function.例如, getName()函数返回一个本地称为data的变量,但您可以通过调用该函数来访问该值。

def getName(user_id):
    data = "Your name is " + str(user_id)
    return data 

# Throws an error, because data in undefined 
name = getName("Bobby")
print(data)

# Working code, prints "Your name is Bobby"
name = getName("Bobby")
print(name)

There are no rules against calling one function from inside another.没有禁止从另一个函数内部调用一个函数的规则。 Instead of a = data.readlines() try a = getFastaFromFile("dna.fasta') as well as changing data = inf.read() to data = inf.readlines()而不是a = data.readlines()尝试a = getFastaFromFile("dna.fasta')以及将data = inf.read()更改为data = inf.readlines()

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

相关问题 如何在另一个函数中使用一个函数收集的数据 - How do I use the data collected of one function in another function 如何从一个函数中获取一个值并在另一个函数中使用它? - How do I take a value from one function and use it in another? 如何在另一个 function 的输入中使用? - How do I use in input from one function in another? python - 如何在不使用返回的情况下从一个函数中获取信息以在python中的另一个函数中使用? - How do I take information from one function to use in another in python without using return? 如何使用python中另一个函数的局部变量? - How do I use local variables from another function in python? 如何在同一类的另一个函数中使用一个函数中的一个对象 - How do I use one object from one function in another function in the same class 如何将一个 function 的 output 用于另一个 function? - How do I use the output of one function for another function? 如何在python中将一个函数中的变量转换为另一个函数? - How do I get a variable in one function to another function in python? 如何在另一个 function 中使用一个 function 的数据? - How to use data from one function in another function? 如何在另一个 function 中使用来自一个 function 的本地定义变量? - How do I use a locally defined variable from one function in another function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM