数组是线性数据结构的集合,包含连续内存空间中相同数据类型的所有元素。它就像一个容器,容纳一定数量的具有相同数据类型的元素。数组的索引从 0 开始,因此,程序员可以很容易地获得每个元素的位置,并对数组执行各种操作。在本节中,我们将学习 Python 中的 2D(二维)数组。
二维阵列(2D 阵列)
2D 数组是一个数组数组,可以像行和列一样以矩阵形式表示。在这个数组中,数据元素的位置是用两个索引而不是一个索引来定义的。 语法
Array_name = [rows][columns] # declaration of 2D array
Arr-name = [ [m1, m2, m3, … . mn], [n1, n2, n3, … .. nn] ]
其中 m 为行,n 为表的列。
访访问二维数组
在 Python 中,我们可以使用两个索引来访问二维数组的元素。第一个索引是指列表的索引,第二个索引是指元素的位置。如果我们只定义一个带数组名的索引,它会返回存储在数组中的所有二维元素。 让我们创建一个简单的程序来理解 Python 中的 2D (二维)数组。 2 DSI example . 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.
输出: 在上面的示例中,我们将 1、0 和 2 作为参数传递到 2D 数组中,该数组打印定义的索引的整行。我们还通过了student _ dt【3】【4】来打印一个特定的元素,它代表元素的二维数组的第 3 个第索引和第 4 个第位置。
穿越 2D 的元素(二维)
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 = " ") # print the elements
print()
输出:
在 2D(二维)数组中插入元素
我们可以使用 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("Before inserting the array elements: ")
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("After inserting the array elements ")
for i in arr1: # Outer loop
for j in i: # inner loop
print(j, end = " ") # print inserted elements.
print()
输出:
更新二维(二维)数组中的元素
在 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("Before inserting the array elements: ")
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("After inserting the array elements ")
for i in arr1: # Outer loop
for j in i: # inner loop
print(j, end = " ") # print inserted elements.
print()
输出:
在 Python 中删除 2D(二维)数组中的值
在二维数组中,我们可以使用 Python 中的 del() 函数移除数组的特定元素或整个索引。让我们理解一个删除元素的例子。 删除. 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("Before Deleting the array elements: ")
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("After Deleting the array elements ")
for i in arr1: # Outer loop
for j in i: # inner loop
print(j, end = " ") # print inserted elements.
print()
输出:
2D 阵列的大小
一个透镜()函数用来得到二维数组的长度。换句话说,我们可以说一个透镜()函数决定了二维数组中可用的总索引。 让我们理解一下在 Python 中获取二维数组大小的 len()函数。 Size.py
array_size = [[1, 3, 2],[2,5,7,9], [2,4,5,6]] # It has 3 index
print("The size of two dimensional array is : ")
print(len(array_size)) # it returns 3
array_def = [[1, 3, 2], [2, 4, 5, 6]] # It has 2 index
print("The size of two dimensional array is : ")
print(len(array_def)) # it returns 2
输出: 写一个程序,用 Python 打印二维数组的和。 矩阵 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 "Enter the matrix [{0}][{j}]"))
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("Sum of the matrix is :")
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("Enter the value of m or Row\n")) # take the rows
n = int(input("Enter the value of n or columns\n")) # take the columns
print("Enter the First matrix ") # print the first matrix
A = two_d_matrix(m, n) # call the matrix function
print("display the first (A) matrix")
print(A) # print the matrix
print("Enter the Second (B) matrix ")
B = two_d_matrix(m, n) # call the matrix function
print("display the Second (B) matrix")
print(B) # print the B matrix
s= sum(A, B) # call the sum function
print(s) # print the sum of A and B matrix.