简体   繁体   English

列表和变量可以从函数中返回吗?

[英]Can a list and variables be returned from a function?

I can do this.. 我可以做这个..

def funcOne():
    a,b = funcTwo()
    print a, b

def funcTwo():
    .....
    ......   
    return x, y

But can a list also be returned from funcTwo and displayed in funcOne ALONG with the 2 values? 但是,列表也可以从funcTwo返回并以2个值显示在funcOne中吗? Nothing seems to work 似乎没什么用

When you return multiple values, all you are doing is building a single tuple containing those values, and returning it. 当您返回多个值时,您要做的就是构建包含这些值的单个元组,然后将其返回。 You can construct tuples with anything in it, and a list comes under anything: 您可以在其中包含任何内容的情况下构造元组,并且列表会出现在任何内容下方:

def funcOne():
    a, b, some_list = funcTwo()
    print a, b, some_list

def funcTwo():
    ...
    some_list = [...] 
    return x, y, some_list

If you mean you wish to return the values from the list, you can do that by just returning the list, unpacking works with lists too: 如果您希望从列表中返回值,则可以通过仅返回列表来完成,解包也可以使用列表:

def funcOne():
    a, b, = funcTwo()
    print a, b

def funcTwo():
    ...
    some_list = [x, y] 
    return some_list

Or if you want to extend the returned values with all the values from a list, you just need to concatenate a list of values you wish to return with the list of extra values: 或者,如果要使用列表中的所有值扩展返回的值,则只需将希望返回的值列表与其他值列表连接在一起:

def funcOne():
    a, b, c, d = funcTwo()
    print a, b, c, d

def funcTwo():
    ...
    some_list = [z, w] 
    return [x, y] + some_list
>>> def list_and_vars():
    a, b = "fee", "fi"
    c = ["fo", "fum"]
    return a, b, c

>>> print list_and_vars()
('fee', 'fi', ['fo', 'fum'])
>>> a, b, [c, d] = list_and_vars()
>>> a, b, c, d
('fee', 'fi', 'fo', 'fum')
def funcOne():
    f2x,f2y = funcTwo() #tuple unpacking var1,var2 = (val1,val2)
    print a,b,list((lf2x,f2y)) #convert tuple to list

def funcTwo(): 
    return x, y #actually returns (x,y)

If we look at the following 如果我们看以下

def funcTwo():
    return 'a','b'
print(funcTwo())

We can see that funcTwo returns a tuple containing 'a','b' : 我们可以看到funcTwo返回一个包含'a','b'元组

>>> 
('a', 'b')

In this way Python does only return one value, however it's tuple unpacking can make it look like this isn't the case: 这样,Python只会返回一个值,但是对它进行元组拆包会使它看起来并非如此:

var1,var2 = funcTwo()
print(var1)
print(var2)
>>> 
a
b

You could return a list explicitly: 可以显式返回一个列表:

def funcTwo():
    return ['a','b']
print(funcTwo())

In which case you are still returning one value, it's simply a list. 在这种情况下,您仍将返回一个值,它只是一个列表。 For example: 例如:

def funcTwo():
    return ['a','b']
def funcOne():
    return 'c','d',funcTwo()
print(funcOne())

Would print 会打印

>>> 
('c', 'd', ['a', 'b'])

Again you can see, we've only returned one value, a tuple which consists of all the values we desired to return. 再次您可以看到,我们只返回了一个值,一个元组,其中包含我们要返回的所有值。

If you wanted to simply print something in funcOne unless you used string formatting you would get the same result as if you had print -ed the return values because print('a','b','c') prints ('a','b','c') however you could do something like: 如果您只是想在funcOne简单地打印某些内容,除非您使用字符串格式,否则您将获得与print -ed返回值相同的结果,因为print('a','b','c')打印('a','b','c')但是您可以执行以下操作:

def funcOne():
    print(','.join(['c','d',funcTwo().__repr__()]))
>>> 
c,d,['a', 'b']

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

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