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/zh-hant/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

發表回復

登錄後才能評論