在 Python 函数中返回多个列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19469697/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 13:48:45  来源:igfitidea点击:

Return multiple lists in Python function

pythonlistfunctionreturn

提问by Christoffer

I am new to Python, and am trying to create a function which creates lists with different material parameters from user inputs as shown in the code below.

我是 Python 新手,正在尝试创建一个函数,该函数根据用户输入创建具有不同材料参数的列表,如下面的代码所示。

def material():
    layers = int(raw_input("State the number of material layers in the wall (0 for default material): "))

    rho = [] # Density [kg/m3]
    c = [] # Heat capacity [J/(kg K)] 
    k = [] # Heat conductivity [W/(m K)]
    #a = [] # Thermal diffusivity [m2/s]
    d = [] # Thickness of material [m]

    # Saveing material properties
    if layers == 0:
        rho.append(2300)
        c.append(900)
        k.append(1.6)
        d.append(3.2)
        layers = 1

    else:
        for i in range(layers):
            print "\n" "Define thermal properties for material", i+1,"(starting from left)"
            rho.append(float(raw_input("Density [kg/m3]: ")))
            c.append(float(raw_input("Heat capacity [J/(kg K)]: ")))
            k.append(float(raw_input("Heat conductivity [W/(m K)]: ")))
            d.append(float(raw_input("Thickness [m]: ")))

    return ???

How should I return rho, c, k, dand layersso I am able to e.g. print – or use the value of – e.g. the second item in the list of d?

我应该如何返回rho、c、k、d图层,以便我能够例如打印 - 或使用 - 例如d列表中的第二项的值?

print d[1]

采纳答案by DSM

How should I return rho, c, k, d and layers [...] ?

我应该如何返回 rho, c, k, d 和 layers [...] ?

Simply do it:

只需这样做:

return rho, c, k, d, layers

And then you'd call it like

然后你会这样称呼它

rho, c, k, d, layers = material()
print d[1]

Note that the more stuff you're returning, the more likely it is you're going to want to wrap it all together into some structure like a dict(or namedtuple, or class, etc.)

请注意,您返回的内容越多,您就越有可能想要将它们全部包装成某种结构,例如dict(or namedtuple, 或 class 等)

回答by DSM

returncan return multiple values if you separate them with commas:

return如果用逗号分隔它们,则可以返回多个值:

return rho, c, k, d, layers

This will make materialreturn a tuple containing rho, c, k, d, and layers.

这将material返回一个包含rho, c, k, d, 和的元组layers

Once this is done, you can access the values returned by materialthrough unpacking:

完成后,您可以material通过解包访问返回的值:

rho, c, k, d, layers = material()

Here is a demonstration:

这是一个演示:

>>> def func():
...     return [1, 2], [3, 4], [5, 6]
...
>>> a, b, c = func()
>>> a
[1, 2]
>>> b
[3, 4]
>>> c
[5, 6]
>>> a[1]
2
>>>

回答by santosh singh

def return_values():
    # your code
    return list1, list2

and get the value like below

并获得如下值

value1, value2 = return_values()

回答by Archit Jain

def function_returning_multiple_list():
  l1=[]
  l2=[]
  l3=[]
  return (l1,l2,l3)


def another_fucntion():
  list1,list2,list3=function_returning_multiple_list()
  print list2