Python では、関数から複数の値を返すことができます。以下はさまざまな方法です 1) オブジェクトの使用: これは C/C++ や Java と似ており、複数の値を保持するクラス (C では struct) を作成し、そのクラスのオブジェクトを返すことができます。
パイソン
# A Python program to return multiple> # values from a method using class> class> Test:> >def> __init__(>self>):> >self>.>str> => 'geeksforgeeks'> >self>.x>=> 20> # This function returns an object of Test> def> fun():> >return> Test()> > # Driver code to test above method> t>=> fun()> print>(t.>str>)> print>(t.x)> |
>
>出力
geeksforgeeks 20>
以下は、C++/Java の世界を変える人にとって興味深いメソッドです。
1/1000.00
2) タプルの使用: タプルは、カンマで区切られた項目のシーケンスです。 () の有無にかかわらず作成されます。タプルは不変です。タプルとリストの詳細についてはこちらをご覧ください。
パイソン
# A Python program to return multiple> # values from a method using tuple> # This function returns a tuple> def> fun():> >str> => 'geeksforgeeks'> >x>=> 20> >return> str>, x># Return tuple, we could also> ># write (str, x)> # Driver code to test above method> str>, x>=> fun()># Assign returned tuple> print>(>str>)> print>(x)> |
>
ローカル日時Java
>出力
geeksforgeeks 20>
3) リストの使用: リストは、角括弧を使用して作成された項目の配列のようなものです。さまざまなタイプの項目を含めることができるため、配列とは異なります。リストは変更可能なため、タプルとは異なります。
パイソン
# A Python program to return multiple> # values from a method using list> # This function returns a list> def> fun():> >str> => 'geeksforgeeks'> >x>=> 20> >return> [>str>, x]> # Driver code to test above method> list> => fun()> print>(>list>)> |
>
>出力
['geeksforgeeks', 20]>
4) 辞書の使用: ディクショナリは、他の言語のハッシュやマップに似ています。辞書の詳細についてはこちらをご覧ください。
パイソン
# A Python program to return multiple> # values from a method using dictionary> # This function returns a dictionary> def> fun():> >d>=> dict>();> >d[>'str'>]>=> 'techcodeview.com'> >d[>'x'>]>=> 20> >return> d> # Driver code to test above method> d>=> fun()> print>(d)> |
>
>出力
{'x': 20, 'str': 'techcodeview.com'}> 5) データ クラスの使用 (Python 3.7 以降): Python 3.7 以降では、データ クラスを使用して、自動的に追加された一意のメソッドを持つクラスを返すことができます。データ クラス モジュールには、デコレーターと、__init__() や __repr__() などの生成された特別なメソッドをユーザー定義クラスに自動的に追加する関数があります。
Python3
スイッチJava
from> dataclasses>import> dataclass> @dataclass> class> Book_list:> >name:>str> >perunit_cost:>float> >quantity_available:>int> => 0> > ># function to calculate total cost> >def> total_cost(>self>)>->>>>float>:> >return> self>.perunit_cost>*> self>.quantity_available> > book>=> Book_list(>'Introduction to programming.'>,>300>,>3>)> x>=> book.total_cost()> # print the total cost> # of the book> print>(x)> # print book details> print>(book)> # 900> Book_list(name>=>'Python programming.'>,> >perunit_cost>=>200>,> >quantity_available>=>3>)> |
>
SQL複数テーブル選択
>出力
900 Book_list(name='Introduction to programming.', perunit_cost=300, quantity_available=3)>
6.「収量」の使用
Python で関数から複数の値を返す別のアプローチの 1 つは、ジェネレーター関数で yield キーワードを使用することです。ジェネレーター関数は、反復子オブジェクトを返す特別なタイプの関数であり、一度に 1 つの値をその場で値のシーケンスを生成します。
ジェネレーター関数から複数の値を返すには、 yield キーワードを使用して各値を順番に生成します。ジェネレーター関数は、次の値が要求されるまで実行を一時停止し、要求された時点で実行を再開し、次の値を生成します。このプロセスは、ジェネレーター関数が実行を完了するか、return ステートメントに遭遇するまで続きます。
これを行う方法の例を次に示します。
Python3
def> get_values():> >yield> 42> >yield> 'hello'> >yield> [>1>,>2>,>3>]> # Test code> result>=> get_values()> print>(>next>(result))># should print 42> print>(>next>(result))># should print 'hello'> print>(>next>(result))># should print [1, 2, 3]> |
>
>出力
42 hello [1, 2, 3]>
時間計算量 : O(1) は、入力のサイズに関係なく、一定数の演算 (結果) のみを実行するためです。
補助スペース: O(1) は、常に一定数の変数 (生成値) のみをメモリに保存するためです。
参照: http://stackoverflow.com/questions/354883/how-do-you-return-multiple-values-in-python