數組是線性數據結構的集合,包含連續內存空間中相同數據類型的所有元素。它就像一個容器,容納一定數量的具有相同數據類型的元素。數組的索引從 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.
輸出:
原創文章,作者:簡單一點,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/128967.html