简体   繁体   English

将值传递给 Python 中的另一个 function?

[英]Passing values to another function in Python?

I currently have 4 functions, of which I need to connect.我目前有 4 个功能,其中我需要连接。 I have the first function, which formats a csv file and turns out 4 values, the numbers of (useful) rows and columns and two different lists.我有第一个 function,它格式化一个 csv 文件并输出 4 个值,(有用的)行数和列数以及两个不同的列表。 Then, the next two functions have to use the values from the first function to calculate some things.然后,接下来的两个函数必须使用第一个 function 的值来计算一些东西。 Finally, the main function (the last one) will take those calculated values and produce an output.最后,主 function(最后一个)将采用这些计算值并生成 output。 I'm really struggling in learning how to pass the output's of the functions to the other ones.我真的很努力学习如何将函数的输出传递给其他函数。 Currently it looks (really roughly) like this:目前它看起来(非常粗略)是这样的:

def formatting():
    ...
    n = 30
    m = 9
    array = [...]
    tot = [...]
    return(n, m, array, tot)

def calculation():
    formatting()
    # a bunch of code that uses n, m, array and tot
    mn = [...]
    mx = [...]
    av = [...]
    sd = [...]
    return(mn, mx, av, sd)

def cor():
   formatting()
   # a bunch of code that uses n, m, array and tot
   rs = [...]
   return(rs)

def main(csvfile):
   calculation()
   cor()
main()

It keeps telling me that there is an error in the first function, saying that m is not defined (first line in which a passed variable is used), meaning that the value from m has not passed to the calculation function and hence won't be usable in any of the other functions.它一直告诉我第一个 function 中有一个错误,说 m 没有定义(第一行使用了传递的变量),这意味着 m 中的值没有传递给计算 function,因此不会可用于任何其他功能。 Can someone explain to me how I can pass this correctly?有人可以向我解释我如何正确传递这个吗?

I also need to have an argument in the main function, which lists the file to be analyzed and I need to pass that onto formatting.我还需要在主 function 中有一个参数,它列出了要分析的文件,我需要将其传递给格式化。

Thank you!谢谢!

You need to assign the return values of formatting to variables before you can use them in the other function, eg您需要将formatting的返回值分配给变量,然后才能在其他 function 中使用它们,例如

def calculation():
    m, n, array, tot = formatting()
    # a bunch of code that uses n, m, array and tot
    mn = [...]
    mx = [...]
    av = [...]
    sd = [...]
    return(mn, mx, av, sd)
def formatting():
    ...
    n = 30
    m = 9
    array = [...]
    tot = [...]
    return(n, m, array, tot)

def calculation():
    n,m,array,tot = formatting()
    # a bunch of code that uses n, m, array and tot
    mn = [...]
    mx = [...]
    av = [...]
    sd = [...]
    return(mn, mx, av, sd)

def cor():
   n,m,array,tot = formatting()
   # a bunch of code that uses n, m, array and tot
   rs = [...]
   return(rs)

def main():
   calculation()
   cor()
main()

When you want to use values from different functions, you should assign them to variable while calling and then use those assigned variables to do calculations.当您想使用来自不同函数的值时,您应该在调用时将它们分配给变量,然后使用这些分配的变量进行计算。

The concept you are looking for is Python packing and unpacking feature.您正在寻找的概念是 Python 打包和拆包功能。

Check this out for details on Packing and Unpacking 查看此以获取有关打包和拆包的详细信息

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM