Python 2D数组

Python 2D数组

一个数组 是线性数据结构的集合,它在连续的内存空间中包含了相同数据类型的所有元素。它就像一个容器,用来存储一定数量的具有相同数据类型的元素。数组的索引从0开始,因此程序员可以轻松获取每个元素的位置,并对数组执行各种操作。在本节中,我们将学习关于Python中的2D(二维)数组。

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(二维)数组。

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.

输出:

Python 2D数组

在上面的例子中,我们将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()

输出:

Python 2D数组

在二维数组中插入元素

我们可以使用 insert() 函数将元素插入到二维数组中,该函数指定要插入的元素的索引号和位置。

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

输出:

Python 2D数组

在二维数组中更新元素

在二维数组中,可以用新的值来更新数组的现有值。在这种方法中,我们可以改变特定值以及数组的整个索引。让我们通过一个二维数组的示例来理解,如下所示。

创建一个用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("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中的二维数组中删除值

在二维数组中,我们可以使用Python中的 del() 函数删除特定元素或整个索引。让我们通过一个例子来理解如何删除一个元素。

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

输出:

Python 2D数组

2D数组的大小

使用 len ()函数可以获取二维数组的长度。换句话说, len ()函数确定了二维数组中可用的总索引。

让我们理解一下如何使用len()函数来获取Python中的二维数组的大小。

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 2D数组

编写一个程序,在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 "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.

输出:

Python 2D数组

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程