Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在用户定义的Python函数中引用大变量的最有效方法?_Python_Python 3.x_Function_Optimization_Arguments - Fatal编程技术网

在用户定义的Python函数中引用大变量的最有效方法?

在用户定义的Python函数中引用大变量的最有效方法?,python,python-3.x,function,optimization,arguments,Python,Python 3.x,Function,Optimization,Arguments,我想知道是否有一种更有效的方法可以从Python中的函数中引用大变量(例如具有数十万条目的数组),而不仅仅是将其作为参数传入?我知道global是一个选项,但它太。。。不可靠,因为没有一个更好的词,我几乎认为它是无关紧要的(除非,也许有人能解释为什么不是这样)。我问这个问题是因为我最近写了一个脚本来调用这个函数: def build(unique,gene,index): ###Concatenates entries from arguments into single string###

我想知道是否有一种更有效的方法可以从Python中的函数中引用大变量(例如具有数十万条目的数组),而不仅仅是将其作为参数传入?我知道
global
是一个选项,但它太。。。不可靠,因为没有一个更好的词,我几乎认为它是无关紧要的(除非,也许有人能解释为什么不是这样)。我问这个问题是因为我最近写了一个脚本来调用这个函数:

def build(unique,gene,index): ###Concatenates entries from arguments into single string###
    ###Builds array from entries in all of unique's sublists###
    hold= []
    hold.append([category[index] for category in unique[1]])
    ###Builds a list of string concatenated from entries in other lists/arrays###
    line= ['\t'.join(gene[0:7]),'\t'.join(hold[0]),'\t'.join(gene[9:len(gene)])]
    ###Concatenates array in a single string###
    line= '\t'.join(line)
    return line
从循环中:

for gene in table[1:]:
    buffer.append(build(unique,gene,table.index(gene)))
变量
unique
是一个包含约500k个条目的数组,循环运行约60k次。我知道这肯定需要一段时间(目前仅此循环大约需要12分钟),但我希望有一种方法可以优化方法,通过该方法,函数中引用
unique
,这样就不必每次都传递大量数组


提前谢谢

这里没有什么大的东西通过
unique
每次都引用相同的列表;函数调用时不复制任何内容


您需要在别处寻找优化。

您正在传递对数组的引用。不像每次函数调用都会创建一个新的副本。您的代码似乎有问题,但您提到的不是其中之一。@cᴏʟᴅsᴘᴇᴇᴅ 你指的是什么问题?我会接受任何我能得到的帮助!