logo

Python 2D 配列

配列 連続したメモリ空間にある同じデータ型のすべての要素を含む線形データ構造のコレクションです。これは、同じデータ型を持つ一定数の要素を保持するコンテナーのようなものです。配列のインデックスは 0 から始まるため、プログラマは各要素の位置を簡単に取得し、配列に対してさまざまな操作を実行できます。このセクションでは、Python の 2D (2 次元) 配列について学びます。

Python 2D 配列

2次元配列(2D配列)

2D配列 行と列のような行列形式で表現できる配列の配列です。この配列では、データ要素の位置が 1 つのインデックスではなく 2 つのインデックスで定義されます。

構文

魅力的なコンピューター言語
 Array_name = [rows][columns] # declaration of 2D array Arr-name = [ [m1, m2, m3, &#x2026; . m<sub>n</sub>], [n1, n2, n3, &#x2026; .. n<sub>n</sub>] ] 

どこ メートル は行であり、 n テーブルの列です。

2次元配列へのアクセス

パイソン 、2 つのインデックスを使用して 2 次元配列の要素にアクセスできます。最初のインデックスはリストのインデックス付けを指し、2 番目のインデックスは要素の位置を指します。配列名を持つインデックスを 1 つだけ定義すると、配列に格納されている 2 次元のすべての要素が返されます。

わかりやすい簡単なプログラムを作ってみましょう 2D Python の (2 次元) 配列。

2dSimple.py

 Student_dt = [ [72, 85, 87, 90, 69], [80, 87, 65, 89, 85], [96, 91, 70, 78, 97], [90, 93, 91, 90, 94], [57, 89, 82, 69, 60] ] #print(student_dt[]) print(Student_dt[1]) # print all elements of index 1 print(Student_dt[0]) # print all elements of index 0 print(Student_dt[2]) # print all elements of index 2 print(Student_dt[3][4]) # it defines the 3rd index and 4 position of the data element. 

出力:

Python 2D 配列

上の例では、定義されたインデックスの行全体を出力する 2D 配列にパラメーターとして 1、0、および 2 を渡しました。そして私たちも合格しました 学生_dt[3][4] それは3を表しますrdインデックスと4番目特定の要素を印刷するための要素の 2 次元配列の位置。

2D (2 次元) での要素の移動

Program.py

Java if else ステートメント
 # write a program to traverse every element of the two-dimensional array in Python. Student_dt = [ [72, 85, 87, 90, 69], [80, 87, 65, 89, 85], [96, 91, 70, 78, 97], [90, 93, 91, 90, 94], [57, 89, 82, 69, 60] ] # Use for loop to print the entire elements of the two dimensional array. for x in Student_dt: # outer loop for i in x: # inner loop print(i, end = &apos; &apos;) # print the elements print() 

出力:

Python 2D 配列

2D (二次元) 配列に要素を挿入する

を使用して要素を 2D 配列に挿入できます。 入れる() 要素のインデックス番号と挿入される位置を指定する関数。

挿入.py

 # Write a program to insert the element into the 2D (two dimensional) array of Python. from array import * # import all package related to the array. arr1 = [[1, 2, 3, 4], [8, 9, 10, 12]] # initialize the array elements. print(&apos;Before inserting the array elements: &apos;) print(arr1) # print the arr1 elements. # Use the insert() function to insert the element that contains two parameters. arr1.insert(1, [5, 6, 7, 8]) # first parameter defines the index no., and second parameter defines the elements print(&apos;After inserting the array elements &apos;) for i in arr1: # Outer loop for j in i: # inner loop print(j, end = &apos; &apos;) # print inserted elements. print() 

出力:

Python 2D 配列

2-D (二次元) 配列内の要素を更新する

2D 配列では、配列の既存の値を新しい値で更新できます。このメソッドでは、特定の値だけでなく、配列のインデックス全体も変更できます。以下に示すような 2D 配列の例で理解してみましょう。

Python で 2D 配列の既存の値を更新するプログラムを作成します。

Update.py

 from array import * # import all package related to the array. arr1 = [[1, 2, 3, 4], [8, 9, 10, 12]] # initialize the array elements. print(&apos;Before inserting the array elements: &apos;) print(arr1) # print the arr1 elements. arr1[0] = [2, 2, 3, 3] # update the value of the index 0 arr1[1][2] = 99 # define the index [1] and position [2] of the array element to update the value. print(&apos;After inserting the array elements &apos;) for i in arr1: # Outer loop for j in i: # inner loop print(j, end = &apos; &apos;) # print inserted elements. print() 

出力:

Python 2D 配列

Python で 2D (2 次元) 配列から値を削除する

2 次元配列では、次のコマンドを使用して、配列の特定の要素またはインデックス全体を削除できます。 ()の Pythonの関数。要素を削除する例を理解してみましょう。

.pyを削除

 from array import * # import all package related to the array. arr1 = [[1, 2, 3, 4], [8, 9, 10, 12]] # initialize the array elements. print(&apos;Before Deleting the array elements: &apos;) print(arr1) # print the arr1 elements. del(arr1[0][2]) # delete the particular element of the array. del(arr1[1]) # delete the index 1 of the 2-D array. print(&apos;After Deleting the array elements &apos;) for i in arr1: # Outer loop for j in i: # inner loop print(j, end = &apos; &apos;) # print inserted elements. print() 

出力:

蜂の巣とは何ですか
Python 2D 配列

2D配列のサイズ

のみ () 関数は 2 次元配列の長さを取得するために使用されます。言い換えれば、 のみ () 関数は、2 次元配列で使用できるインデックスの合計を決定します。

Python で 2 次元配列のサイズを取得する len() 関数を理解しましょう。

サイズ.py

 array_size = [[1, 3, 2],[2,5,7,9], [2,4,5,6]] # It has 3 index print(&apos;The size of two dimensional array is : &apos;) print(len(array_size)) # it returns 3 array_def = [[1, 3, 2], [2, 4, 5, 6]] # It has 2 index print(&apos;The size of two dimensional array is : &apos;) print(len(array_def)) # it returns 2 

出力:

Python 2D 配列

Python で 2 次元配列の合計を出力するプログラムを作成します。

マトリックス.py

 def two_d_matrix(m, n): # define the function Outp = [] # initially output matrix is empty for i in range(m): # iterate to the end of rows row = [] for j in range(n): # j iterate to the end of column num = int(input(f &apos;Enter the matrix [{0}][{j}]&apos;)) row.append(num) # add the user element to the end of the row Outp.append(row) # append the row to the output matrix return Outp def sum(A, B): # define sum() function to add the matrix. output = [] # initially, it is empty. print(&apos;Sum of the matrix is :&apos;) for i in range(len(A)): # no. of rows row = [] for j in range(len(A[0])): # no. of columns row.append(A[i][j] + B[i][j]) # add matrix A and B output.append(row) return output # return the sum of both matrix m = int(input(&apos;Enter the value of m or Row
&apos;)) # take the rows n = int(input(&apos;Enter the value of n or columns
&apos;)) # take the columns print(&apos;Enter the First matrix &apos;) # print the first matrix A = two_d_matrix(m, n) # call the matrix function print(&apos;display the first (A) matrix&apos;) print(A) # print the matrix print(&apos;Enter the Second (B) matrix &apos;) B = two_d_matrix(m, n) # call the matrix function print(&apos;display the Second (B) matrix&apos;) print(B) # print the B matrix s= sum(A, B) # call the sum function print(s) # print the sum of A and B matrix. 

出力:

Python 2D 配列