logo

Python のネストされたタプル

ネストされたタプルは、別のタプルの内側に配置された Python タプルです。次の 8 要素タプルを見てみましょう。

 tuple = (12, 23, 36, 20, 51, 40, (200, 240, 100)) 

この最後の要素は、括弧で囲まれた 3 つの項目で構成され、別のタプル内に含まれるため、ネストされたタプルとして知られています。インデックス値を持つメイン タプルの名前、tuple[index] を使用してネストされたタプルを取得でき、tuple[index-1][index-2] を使用してネストされたタプルの各項目にアクセスできます。

ネストされたタプルの例

コード

 # Python program to create a nested tuple # Creating a nested tuple of one element only employee = ((10, 'Itika', 13000),) print(employee) # Creating a multiple-value nested tuple employee = ((10, 'Itika', 13000), (24, 'Harry', 15294), (15, 'Naill', 20001), (40, 'Peter', 16395)) print(employee) 

出力:

 ((10, 'Itika', 13000),) ((10, 'Itika', 13000), (24, 'Harry', 15294), (15, 'Naill', 20001), (40, 'Peter', 16395)) 

入れ子になったタプルの一部の操作

入れ子になったタプルに必要な 2 つの操作を見ていきます。

タプルをネストされたタプルに連結する

タプルを扱う場合、場合によっては、個別のレコードを独立した要素として保持しながら、ネストされたグループに変換する必要があります。多くの場合、タプルはコンテンツを追加することによって追加されますが、これにより結果として得られるコンテナーが平坦になりますが、これは通常は望ましくありません。この問題を解決するためのいくつかのアプローチについて話しましょう。

初期化中に + 演算子と「,」を使用する

リッスンポート

この手法では、タプルのメンバーを追加しますが、タプルを初期化するときに、追加中のフラット化を防ぐために各タプルの後にカンマを追加します。

コード

 # Python program to concatenate tuples to make a nested tuple # initializing the tuples tup1 = (5, 4), tup2 = (1, 6), # printing the original tuples print('Tuple 1 : ' + str(tup1)) print('Tuple 2 : ' + str(tup2)) # Concatenating the two tuples to a nested tuple using the + operator nested = tup1 + tup2 # printing the result print('The nested tuple : ' + str(nested)) 

出力:

 Tuple 1 : ((5, 4),) Tuple 2 : ((1, 6),) The nested tuple : ((5, 4), (1, 6)) 

「,」演算子の使用

このタスクは、連結中に「,」演算子を適用することで実行できます。安全な連結が可能です。

コード

 # Python program to concatenate tuples to make a nested tuple # initializing the tuples tup1 = (5, 4) tup2 = (1, 6) # printing the original tuples print('Tuple 1 : ' + str(tup1)) print('Tuple 2 : ' + str(tup2)) # Concatenating the tuples by using the ', 'operator after tuples nested = ((tup1, ) + (tup2, )) # printing result print('The nested tuple ' + str(nested)) 

出力:

 Tuple 1 : (5, 4) Tuple 2 : (1, 6) The nested tuple ((5, 4), (1, 6)) 

入れ子になったタプルの並べ替え

指定されたタプルをソートするには、sorted() メソッドを使用できます。デフォルトでは、このメソッドはタプルを昇順にソートします。たとえば、print(sorted(employee)) は、すべてのネストされたタプルの 0 番目のメンバーとして表示される識別番号に従って、タプル 'employee' を配置します。ラムダ関数を使用すると、ネストされたタプルの最初と 2 番目のメンバーである従業員名やカウントなど、ネストされたタプルの他の要素に応じてタプルを並べ替えることができます。 print(sorted(employee, key = lambda) x: x[1]))。

私のモニターのサイズ

この場合、キーは、どの要素に基づいてタプルをソートする必要があるかを、sorted() 関数に指示します。ラムダ式: lambda x: x[1] は、インデックス 1 要素であるキーを並べ替えの対象として考慮する必要があることを意味します。ラムダ式を lambda x: x[2] として記述して、単語数に従ってタプルを並べ替えることができます。

コード

 # Python program to sort the nested tuple using the sorted() function # Creating a nested tuple employee = ((10, 'Itika', 13000), (24, 'Harry', 15294), (15, 'Naill', 20001), (40, 'Peter', 16395)) # Sorting the tuple by default on the id print(sorted(employee)) # Sorting the tuple on id in reverse order print(sorted(employee, reverse = True)) # Sorting the tuple on name using lambda function print(sorted(employee, key = lambda x: x[1])) # Sorting the tuple on the name in reverse order print(sorted(employee, key = lambda x: x[1], reverse = True)) # Sorting the tuple on the word count print(sorted(employee, key = lambda x: x[2])) # Sorting the tuple on the word count in reverse print(sorted(employee, key = lambda x: x[2], reverse = True)) 

出力:

 [(10, 'Itika', 13000), (15, 'Naill', 20001), (24, 'Harry', 15294), (40, 'Peter', 16395)] [(40, 'Peter', 16395), (24, 'Harry', 15294), (15, 'Naill', 20001), (10, 'Itika', 13000)] [(24, 'Harry', 15294), (10, 'Itika', 13000), (15, 'Naill', 20001), (40, 'Peter', 16395)] [(40, 'Peter', 16395), (15, 'Naill', 20001), (10, 'Itika', 13000), (24, 'Harry', 15294)] [(10, 'Itika', 13000), (24, 'Harry', 15294), (40, 'Peter', 16395), (15, 'Naill', 20001)] [(15, 'Naill', 20001), (40, 'Peter', 16395), (24, 'Harry', 15294), (10, 'Itika', 13000)]