Python 2D 数组

数组是线性数据结构的集合,包含连续内存空间中相同数据类型的所有元素。它就像一个容器,容纳一定数量的具有相同数据类型的元素。数组的索引从 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/n/128967.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
简单一点的头像简单一点
上一篇 2024-10-03 23:25
下一篇 2024-10-03 23:25

相关推荐

  • Python列表中负数的个数

    Python列表是一个有序的集合,可以存储多个不同类型的元素。而负数是指小于0的整数。在Python列表中,我们想要找到负数的个数,可以通过以下几个方面进行实现。 一、使用循环遍历…

    编程 2025-04-29
  • 如何查看Anaconda中Python路径

    对Anaconda中Python路径即conda环境的查看进行详细的阐述。 一、使用命令行查看 1、在Windows系统中,可以使用命令提示符(cmd)或者Anaconda Pro…

    编程 2025-04-29
  • Python周杰伦代码用法介绍

    本文将从多个方面对Python周杰伦代码进行详细的阐述。 一、代码介绍 from urllib.request import urlopen from bs4 import Bea…

    编程 2025-04-29
  • Python计算阳历日期对应周几

    本文介绍如何通过Python计算任意阳历日期对应周几。 一、获取日期 获取日期可以通过Python内置的模块datetime实现,示例代码如下: from datetime imp…

    编程 2025-04-29
  • Python中引入上一级目录中函数

    Python中经常需要调用其他文件夹中的模块或函数,其中一个常见的操作是引入上一级目录中的函数。在此,我们将从多个角度详细解释如何在Python中引入上一级目录的函数。 一、加入环…

    编程 2025-04-29
  • python强行终止程序快捷键

    本文将从多个方面对python强行终止程序快捷键进行详细阐述,并提供相应代码示例。 一、Ctrl+C快捷键 Ctrl+C快捷键是在终端中经常用来强行终止运行的程序。当你在终端中运行…

    编程 2025-04-29
  • 蝴蝶优化算法Python版

    蝴蝶优化算法是一种基于仿生学的优化算法,模仿自然界中的蝴蝶进行搜索。它可以应用于多个领域的优化问题,包括数学优化、工程问题、机器学习等。本文将从多个方面对蝴蝶优化算法Python版…

    编程 2025-04-29
  • Python字典去重复工具

    使用Python语言编写字典去重复工具,可帮助用户快速去重复。 一、字典去重复工具的需求 在使用Python编写程序时,我们经常需要处理数据文件,其中包含了大量的重复数据。为了方便…

    编程 2025-04-29
  • Python程序需要编译才能执行

    Python 被广泛应用于数据分析、人工智能、科学计算等领域,它的灵活性和简单易学的性质使得越来越多的人喜欢使用 Python 进行编程。然而,在 Python 中程序执行的方式不…

    编程 2025-04-29
  • Python清华镜像下载

    Python清华镜像是一个高质量的Python开发资源镜像站,提供了Python及其相关的开发工具、框架和文档的下载服务。本文将从以下几个方面对Python清华镜像下载进行详细的阐…

    编程 2025-04-29

发表回复

登录后才能评论