logo

Tablica 2D Pythona

Tablica to zbiór liniowych struktur danych, które zawierają wszystkie elementy tego samego typu danych w ciągłej przestrzeni pamięci. To jest jak kontener, który przechowuje pewną liczbę elementów o tym samym typie danych. Indeks tablicy zaczyna się od 0, dzięki czemu programista może łatwo uzyskać położenie każdego elementu i wykonać różne operacje na tablicy. W tej sekcji dowiemy się o tablicach 2D (dwuwymiarowych) w Pythonie.

Tablica 2D Pythona

Szyk dwuwymiarowy (szyk 2D)

Tablica 2D to tablica tablic, które można przedstawić w postaci macierzy, takiej jak wiersze i kolumny. W tej tablicy pozycja elementów danych jest definiowana za pomocą dwóch indeksów, a nie pojedynczego indeksu.

Składnia

Java do char
 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>] ] 

Gdzie M to rząd i N jest kolumną tabeli.

Uzyskaj dostęp do tablicy dwuwymiarowej

W Pyton , możemy uzyskać dostęp do elementów dwuwymiarowej tablicy za pomocą dwóch indeksów. Pierwszy indeks odnosi się do indeksowania listy, drugi zaś do położenia elementów. Jeśli zdefiniujemy tylko jeden indeks z nazwą tablicy, zwróci on wszystkie elementy 2-wymiarowe zapisane w tablicy.

Stwórzmy prosty program do zrozumienia 2D (dwuwymiarowe) tablice w Pythonie.

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. 

Wyjście:

Tablica 2D Pythona

W powyższym przykładzie przekazaliśmy 1, 0 i 2 jako parametry do tablicy 2D, która wypisuje cały wiersz zdefiniowanego indeksu. I my też przeszliśmy uczeń_dt[3] [4] który reprezentuje 3r & Dindeks i 4tpozycja dwuwymiarowej tablicy elementów do wydrukowania określonego elementu.

Przechodzenie elementu w 2D (dwuwymiarowe)

Program.py

 # 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() 

Wyjście:

wartość logiczna na ciąg
Tablica 2D Pythona

Wstaw elementy w szyku 2D (dwuwymiarowym).

Możemy wstawiać elementy do tablicy 2D za pomocą wstawić() funkcja określająca numer indeksu elementu i położenie, które ma zostać wstawione.

Wstaw.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() 

Wyjście:

Tablica 2D Pythona

Aktualizuj elementy w tablicy 2-D (dwuwymiarowej).

W tablicy 2D istniejąca wartość tablicy może zostać zaktualizowana o nową wartość. W tej metodzie możemy zmienić konkretną wartość, a także cały indeks tablicy. Rozważmy to na przykładzie tablicy 2D, jak pokazano poniżej.

Utwórz program aktualizujący istniejącą wartość tablicy 2D w Pythonie.

Aktualizacja.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() 

Wyjście:

Tablica 2D Pythona

Usuń wartości z tablicy 2D (dwuwymiarowej) w Pythonie

W tablicy 2-D możemy usunąć konkretny element lub cały indeks tablicy za pomocą z() funkcja w Pythonie. Rozumiemy przykład usunięcia elementu.

Usuń.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() 

Wyjście:

Tablica 2D Pythona

Rozmiar tablicy 2D

A tylko Funkcja () służy do uzyskania długości tablicy dwuwymiarowej. Innymi słowy możemy powiedzieć, że a tylko Funkcja () określa całkowity indeks dostępny w tablicach dwuwymiarowych.

lista tablic posortowana

Przyjrzyjmy się funkcji len(), aby uzyskać rozmiar dwuwymiarowej tablicy w Pythonie.

Rozmiar.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 

Wyjście:

Tablica 2D Pythona

Napisz program wyświetlający sumę tablic dwuwymiarowych w języku Python.

Matrix.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. 

Wyjście:

Tablica 2D Pythona