logo

Python 2D-array

En uppsättning är en samling linjära datastrukturer som innehåller alla element av samma datatyp i angränsande minnesutrymme. Det är som en behållare som innehåller ett visst antal element som har samma datatyp. En arrays index börjar på 0, och därför kan programmeraren enkelt få positionen för varje element och utföra olika operationer på arrayen. I det här avsnittet kommer vi att lära oss om 2D (tvådimensionella) arrayer i Python.

Python 2D-array

Tvådimensionell matris (2D-matris)

En 2D-array är en array av arrayer som kan representeras i matrisform som rader och kolumner. I denna matris definieras positionen för dataelement med två index istället för ett enda index.

Syntax

Java anonym funktion
 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>] ] 

Var m är raden och n är kolumnen i tabellen.

Få tillgång till tvådimensionell array

I Pytonorm , kan vi komma åt element i en tvådimensionell array med hjälp av två index. Det första indexet hänvisar till indexeringen av listan och det andra indexet hänvisar till elementens position. Om vi ​​bara definierar ett index med ett arraynamn returnerar det alla element i 2-dimensionellt lagrade i arrayen.

Låt oss skapa ett enkelt program att förstå 2D (tvådimensionella) arrayer i 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. 

Produktion:

Python 2D-array

I exemplet ovan skickade vi 1, 0 och 2 som parametrar till en 2D-array som skriver ut hela raden i det definierade indexet. Och vi har också passerat student_dt[3][4] som representerar 3:anrdindex och 4thpositionen för en 2-dimensionell array av element för att skriva ut ett visst element.

Att korsa elementet i 2D (tvådimensionellt)

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

Produktion:

Python 2D-array

Infoga element i en 2D (tvådimensionell) array

Vi kan infoga element i en 2D-array med hjälp av Föra in() funktion som anger elementets indexnummer och plats som ska infogas.

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

Produktion:

Python 2D-array

Uppdatera element i en 2-D (tvådimensionell) array

I en 2D-matris kan det befintliga värdet för matrisen uppdateras med ett nytt värde. I den här metoden kan vi ändra det specifika värdet såväl som hela indexet för arrayen. Låt oss förstå med ett exempel på en 2D-array, som visas nedan.

Skapa ett program för att uppdatera det befintliga värdet för en 2D-array i 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() 

Produktion:

turbo c++ ladda ner
Python 2D-array

Ta bort värden från en 2D (tvådimensionell) array i Python

I en 2-D-array kan vi ta bort det specifika elementet eller hela indexet för matrisen med hjälp av av() funktion i Python. Låt oss förstå ett exempel för att ta bort ett element.

Delete.py

rensa cache npm
 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() 

Produktion:

Python 2D-array

Storleken på en 2D-array

A endast () funktion används för att få längden på en tvådimensionell matris. Med andra ord kan vi säga att a endast ()-funktionen bestämmer det totala indexet som är tillgängligt i 2-dimensionella arrayer.

Låt oss förstå len()-funktionen för att få storleken på en 2-dimensionell array i Python.

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

Produktion:

Python 2D-array

Skriv ett program för att skriva ut summan av de 2-dimensionella arrayerna i 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. 

Produktion:

Python 2D-array