logo

Matrice 2D Python

O matrice este o colecție de structuri de date liniare care conțin toate elementele de același tip de date în spațiul de memorie contiguu. Este ca un container care deține un anumit număr de elemente care au același tip de date. Indicele unui tablou începe de la 0 și, prin urmare, programatorul poate obține cu ușurință poziția fiecărui element și poate efectua diverse operații asupra matricei. În această secțiune, vom afla despre tablourile 2D (două dimensiuni) în Python.

Matrice 2D Python

Matrice bidimensională (Matrice 2D)

O matrice 2D este o matrice de matrice care poate fi reprezentată sub formă de matrice, cum ar fi rânduri și coloane. În această matrice, poziția elementelor de date este definită cu doi indici în loc de un singur index.

Sintaxă

numele produselor de machiaj
 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>] ] 

Unde m este rândul și n este coloana tabelului.

Accesați matrice bidimensională

În Piton , putem accesa elemente ale unui tablou bidimensional folosind doi indici. Primul index se referă la indexarea listei, iar al doilea index se referă la poziția elementelor. Dacă definim un singur index cu un nume de matrice, acesta returnează toate elementele bidimensionale stocate în matrice.

Să creăm un program simplu de înțeles 2D tablouri (bidimensionale) în Python.

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. 

Ieșire:

Matrice 2D Python

În exemplul de mai sus, am trecut 1, 0 și 2 ca parametri într-o matrice 2D care imprimă întregul rând al indexului definit. Și am trecut și noi student_dt[3][4] care reprezintă 3rdindex și 4thpoziţia unei matrice bidimensionale de elemente pentru a imprima un anumit element.

Traversarea elementului în 2D (bidimensional)

Program.py

înlocuire șir de caractere javascript
 # 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() 

Ieșire:

Matrice 2D Python

Inserați elemente într-o matrice 2D (două dimensiuni).

Putem insera elemente într-o matrice 2 D folosind introduce() funcția care specifică numărul de index al elementului și locația care urmează să fie inserată.

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

Ieșire:

Matrice 2D Python

Actualizați elementele într-o matrice 2-D (două dimensiuni).

Într-o matrice 2D, valoarea existentă a matricei poate fi actualizată cu o nouă valoare. În această metodă, putem modifica valoarea particulară, precum și întregul index al matricei. Să înțelegem cu un exemplu de matrice 2D, așa cum se arată mai jos.

Creați un program pentru a actualiza valoarea existentă a unei matrice 2D în Python.

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

Ieșire:

Matrice 2D Python

Ștergeți valori dintr-o matrice 2D (bidimensională) în Python

Într-o matrice 2-D, putem elimina elementul particular sau întregul index al matricei folosind din () funcție în Python. Să înțelegem un exemplu de ștergere a unui element.

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

Ieșire:

if else declarație java
Matrice 2D Python

Dimensiunea unei matrice 2D

A numai Funcția () este utilizată pentru a obține lungimea unui tablou bidimensional. Cu alte cuvinte, putem spune că a numai () determină indicele total disponibil în tablouri bidimensionale.

Să înțelegem funcția len() pentru a obține dimensiunea unui tablou bidimensional în Python.

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

Ieșire:

Matrice 2D Python

Scrieți un program pentru a tipări suma matricelor bidimensionale în 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. 

Ieșire:

Matrice 2D Python