简体   繁体   English

将列表理解传递给Python中的函数

[英]Passing List Comprehension to a function in Python

I have a function in which I would like to pass list comprehension to as an input. 我有一个函数,希望将列表理解作为输入传递给我。 I'm getting an error about my_list not being defined. 我收到有关未定义my_list的错误。 I know I could put my_list outside the function, but in reality that list is generated as part of the function. 我知道我可以将my_list放在函数之外,但实际上,列表是作为函数的一部分生成的。

The actual definition is complex so here is simplified example: 实际定义很复杂,因此这里是简化示例:

def my_def(list_comp):
    global my_list
    my_list = [[1,2],[3,4]]
    list_comp_output = list_comp
    return list_comp_output

print my_def([x[1] for x in my_list])

Actually all we have in Python is runtime; 实际上, 我们在Python中拥有的只是运行时。 there is no such thing as a separate compile time 1 .(in the scope of interpreter). 不存在单独的编译时间 1 (在解释器范围内)。 and the functions are not exception from this rule. 并且功能也不例外。

So what you have done here is defining my_list as a global variable and attempt to use it in a list comprehension, when python doesn't defined such thing. 因此,您在此处所做的就是将my_list定义为全局变量,并在python未定义此类内容时尝试在列表理解中使用它。

You can just run your function 1 time then use that list comprehension : 您可以只运行函数1次,然后使用该列表理解:

def my_def(list_comp):
    global my_list
    my_list = [[1,2],[3,4]]
    list_comp_output = list_comp
    return list_comp_output


my_def([])
print my_def([x[1] for x in my_list])
[2,4]

Also i don't see any thing logical here :) if you want to use a global variable just define if in the scope of your module (out side the function and pass it to your function.) 另外我在这里看不到任何逻辑的东西:)如果您想使用全局变量,只需定义是否在模块范围内(在函数之外并将其传递给函数)。

def my_def(list_comp):
    # do stuff with list_comp
    return list_comp_output


my_list= # a costume list 
li=[x[1] for x in my_list]
print my_def(li)

Or more elegant do the list comprehension within your function : 或者更优雅地在函数中进行列表理解:

def my_def(list_comp):
    return [x[1] for x in list_comp]

1. Learning Python by Mark Lutz 1. Mark Lutz学习Python

That's because my_list is not defined;) 那是因为未定义my_list;)

First you have to realize that the body in the function definition (however if there were default arguments, these would have been evaluated right away) is not executed until the function is actually call, but the argument to it is evaluated before the function is called. 首先,您必须意识到函数定义中的主体(但是,如果有默认参数,则它们将立即被评估)直到实际调用该函数时才执行,但是在调用该函数之前先评估它的参数。

The two first statements in it says first that the symbol my_list is that of the global scope in this function. 它的前两个语句首先说符号my_list是此函数中全局范围的符号。 The second says to assign to that symbol (in global scope) the value [[1,2],[3,4]] . 第二个表示将符号[[1,2],[3,4]]分配给该符号(在全局范围内)。 But this does as mentioned not happen before the function is called (so right after the function definition it is still not defined). 但这确实没有提到在调用函数之前发生(因此,在函数定义之后,它仍然没有定义)。

The consequence of this is that you try to evaluate my_list (when calling the function) before it is defined or assigned to (in the function body). 这样的结果是,您尝试在my_list定义或分配给my_list之前(在函数体内)求值。

You could try to make it happen by calling my_def([]) first, which will define my_list , but the correct way would probably be to put the definition of my_list outside the function definition: 您可以尝试通过首先调用my_def([])来实现它,该方法将定义my_list ,但是正确的方法可能是将my_list的定义放在函数定义之外:

my_list = [[1,2],[3,4]]

def my_def(list_comp):
   list_comp_output = list_comp
   return list_comp_output

print my_def([x[1] for x in my_list])

Which gives the answer: 给出答案:

[2, 4]

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

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