logo

Питхон 2Д низ

Низ је колекција линеарних структура података које садрже све елементе истог типа података у суседном меморијском простору. То је као контејнер који садржи одређени број елемената који имају исти тип података. Индекс низа почиње од 0, и стога програмер може лако да добије позицију сваког елемента и изврши различите операције на низу. У овом одељку ћемо научити о 2Д (дводимензионалним) низовима у Питхон-у.

Питхон 2Д низ

Дводимензионални низ (2Д низ)

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>] ] 

Где м је ред и н је колона табеле.

Приступите дводимензионалном низу

Ин Питхон , можемо приступити елементима дводимензионалног низа користећи два индекса. Први индекс се односи на индексирање листе, а други индекс се односи на позицију елемената. Ако дефинишемо само један индекс са именом низа, он враћа све елементе 2-димензионалног ускладиштеног у низу.

Хајде да направимо једноставан програм за разумевање (дводимензионални) низови у Питхон-у.

2дСимпле.пи

 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. 

Излаз:

Питхон 2Д низ

У горњем примеру, пренели смо 1, 0 и 2 као параметре у 2Д низ који штампа цео ред дефинисаног индекса. И ми смо такође прошли студент_дт[3][4] који представља 3рдиндекс и 4тхположај 2-димензионалног низа елемената за штампање одређеног елемента.

Прелазак елемента у 2Д (дводимензионално)

Програм.пи

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

Излаз:

Питхон 2Д низ

Уметните елементе у 2Д (дводимензионални) низ

Можемо да убацимо елементе у 2 Д низ користећи инсерт() функција која специфицира индексни број и локацију елемента који ће бити уметнути.

Инсерт.пи

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

Излаз:

Питхон 2Д низ

Ажурирајте елементе у 2-Д (дводимензионалном) низу

У 2Д низу, постојећа вредност низа се може ажурирати новом вредношћу. У овој методи можемо променити одређену вредност као и цео индекс низа. Хајде да разумемо са примером 2Д низа, као што је приказано у наставку.

Креирајте програм за ажурирање постојеће вредности 2Д низа у Питхон-у.

алгоритми за сортирање спајањем сортирања

Упдате.пи

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

Излаз:

Питхон 2Д низ

Избришите вредности из 2Д (дводимензионалног) низа у Питхон-у

У 2-Д низу, можемо уклонити одређени елемент или цео индекс низа користећи од() функција у Питхон-у. Хајде да разумемо пример за брисање елемента.

Делете.пи

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

Излаз:

Питхон 2Д низ

Величина 2Д низа

А само () функција се користи за добијање дужине дводимензионалног низа. Другим речима, можемо рећи да а само () функција одређује укупан индекс доступан у 2-димензионалним низовима.

Хајде да разумемо функцију лен() да бисмо добили величину 2-димензионалног низа у Питхон-у.

Величина.пи

 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 

Излаз:

Питхон 2Д низ

Напишите програм за штампање збира 2-димензионалних низова у Питхон-у.

Матрик.пи

јава сортирање низова
 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. 

Излаз:

Питхон 2Д низ