简体   繁体   English

Python'IF'之后的第二条语句没有动作

[英]Python No Action on 2nd Statement After 'IF'

I am trying to loop over a dictionary and call a function on each key. 我试图循环字典并在每个键上调用一个函数。

If the function does not return None entry for that key, I want the output to be appended to a list and I also want that key to be appended to a second list. 如果该函数没有为该键返回None项,则我希望将输出附加到列表中,并且还希望将该键附加到第二个列表中。

Here is my code: 这是我的代码:

    output_list = []
    key_list =[]

    for i in dict.keys():
        if obj.method(i):
            output_list.append(obj.method(i))
            key_list.append(i)

    return output_list
    return key_list

However, for some reason the second list, key_list , is never populated - can you not have two statements below an if like the above? 但是,由于某种原因,第二个列表key_list永远不会被填充-您是否不能像上面的if一样在下面添加两个语句?

The reason that I am doing this is that I want to eventually produce an output where each key of the dict is listed alongside it's associated function ouput, whenever the output is not None . 我这样做的原因是,我希望最终产生一个输出,该dict的每个键都在其相关函数输出旁边列出,只要输出不是None

key_list is being populated, however you can only return once from a function. key_list正在填充,但是您只能从函数返回一次。

You can return a tuple of results though to return multiple variables. 您可以返回结果元组,但可以返回多个变量。 Change: 更改:

return output_list
return key_list

to

return output_list, key_list

And in the calling code do: 并在调用代码中执行:

output_list, key_list = my_function(...)

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

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