[英]How to return multiple values from one function to another in Python?
[英]Return multiple values from one function in python
我可以從一個 function 返回多個值並將它們全部返回到另一個 function 嗎?
def function_1():
one = "Hello"
two = "how"
three = "are"
four = "you"
return one, two, three, four
def function_2(one,two,three,four)
print(one,two,three,four)
function_1()
function_2(one,two,three,four)
我可以從一個 function 返回多個值,並將每個值分配給每個值特定的 function 以執行某些操作嗎? 這在 Python 中是如何解決的?
def function_1():
if something:
one = "Hello"
return one
elif something:
two = "how"
return two
elif something:
three = "are"
return three
elif something:
four = "you"
return four
def function_for_one(one)
print(one)
def function_for_two(two)
print(two)
def function_for_three(three)
print(three)
def function_for_four(four)
print(four)
function_1()
function_for_one(one)
function_for_two(two)
function_for_three(three)
function_for_four(four)
首先,您在function_1()
中有一個小錯誤,您返回的是未定義的變量hello, how, are, you
,而不是return "hello", "how", "are", "you"
。 或者,您可以通過return one, two, three, four
來返回存儲這些字符串的變量。 另外,請注意選項卡,您編寫它的方式是在 function_1() 內部定義 function_2()。
至於你的第一個問題,有幾種方法可以做到。 一個是在第二個 function 接受與第一個返回的 arguments 相同數量的情況下解析第二個中的第一個 function:
function_2(function_1())
第二種方法是存儲第一個 function 返回的值,然后將它們解析為第二個 function,如:
a, b, c, d = function_1()
function_2(a, b, c, d)
有了這個,您可以將它們單獨解析為不同的函數:
function_for_one(a)
function_for_two(b)
function_for_three(c)
等等。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.