Python 中將矩陣轉換為座標字典


字典是四種資料型別中最流行的一種,這四種資料型別都用於儲存無序的鍵值對集合。Python 矩陣將用於表示列表的列表,而內部列表表示矩陣的行值。座標字典定義為元組,透過賦予座標值來設定行和列。在 Python 中,我們有一些內建函式,如 len()、range()、zip() 等,可以用來解決將矩陣轉換為座標字典的問題。

語法

以下語法在示例中使用 -

len()

len() 是 Python 中的一個內建方法,它返回物件的長度。

range()

range() 是 Python 中的一個內建函式,它返回給定範圍內的元素序列。預設情況下,初始範圍始終從 0 開始,並透過分配特定範圍結束。

zip()

zip() 是 Python 中的一個內建函式,可用於組合來自可迭代物件的兩個或多個元素。

enumerate()

Python 的內建函式 enumerate() 允許遍歷有序序列以跟蹤每個索引元素。

argwhere()

argwhere() 是 Python 中一個內建方法,它返回元素索引作為非零值。

nonzero()

nonzero() 是 Python 中一個內建函式,可用於查詢陣列的索引。

使用巢狀迴圈

在以下示例中,程式使用巢狀 for 迴圈來迭代行和列。使用 if 語句,它將條件設定為矩陣值不等於零,這滿足包含零元素作為座標對和非零元素作為值對的鍵。最後,它將使用 return 函式顯示特定結果。

示例

def matrix_to_coordinate_dict(matrix):
    coord_dict = {}
    rows = len(matrix)
    cols = len(matrix[0])
    for i in range(rows):
        for j in range(cols):
            value = matrix[i][j]
            if value != 0:
                coord_dict[(i, j)] = value
    return coord_dict

# Create the matrix
my_matrix = [
    [0, 1, 0],
    [2, 0, 3],
    [0, 4, 0]
]
coord_dict = matrix_to_coordinate_dict(my_matrix)
print(coord_dict)

輸出

 Conversion of Matrix into Coordinate:
 {(0, 1): 1, (1, 0): 2, (1, 2): 3, (2, 1): 4}

使用列表推導式和 enumerate()

在以下示例中,程式使用字典推導式迭代輸入矩陣。非零元素值使用行和列索引作為鍵新增到座標字典中。使用 enumerate(),它跟蹤輸入矩陣中存在的各個元素迭代。最後,該函式返回結果作為鍵(元組)和值對(非零元素)。

示例

def matrix_to_coordinate_dict(matrix):
    coord_dict = {(i, j): value for i, row in enumerate(matrix) for j, value in enumerate(row) if value != 0}
    return coord_dict

# Create the matrix
my_matrix = [
    [0, 1, 0],
    [2, 0, 3],
    [0, 4, 0]
]
coord_dict = matrix_to_coordinate_dict(my_matrix)
print("Conversion of Matrix into Coordinate:\n", coord_dict)

輸出

 Conversion of Matrix into Coordinate:
 {(0, 1): 1, (1, 0): 2, (1, 2): 3, (2, 1): 4}

使用 Numpy 和 zip()

在以下示例中,程式以 numpy 模組和物件引用為 np 開始。然後使用遞迴函式根據將矩陣轉換為座標字典來設定條件和操作。然後它將使用字典推導式,其中它在輸入矩陣上迭代行和列索引。接下來,該函式返回陣列的元組作為鍵,並將值對設定為非零元素並顯示結果。

示例

import numpy as np

def matrix_to_coordinate_dict(matrix):
    indices = np.nonzero(matrix)
    coord_dict = {(i, j): matrix[i][j] for i, j in zip(indices[0], indices[1])}
    return coord_dict

# Create the matrix
my_matrix = np.array([
    [0, 1, 0],
    [2, 0, 3],
    [0, 4, 0]
])
coord_dict = matrix_to_coordinate_dict(my_matrix)
print(coord_dict)

輸出

 {(0, 1): 1, (1, 0): 2, (1, 2): 3, (2, 1): 4}

使用 Numpy 和 numpy.argwhere()

在以下示例中,程式使用 numpy 模組將物件引用設定為 np。它使用內建函式 argwhere() 查詢矩陣中的非零元素。在結果字典中,鍵採用元組形式表示座標,而值設定為矩陣的非零元素。

示例

import numpy as np
# Recursive function
def matrix_to_coordinate_dict(matrix):
    indices = np.argwhere(matrix != 0)
    coord_dict = {(i, j): matrix[i][j] for i, j in indices}
    return coord_dict

# Create the matrix
my_matrix = np.array([
    [0, 1, 0],
    [2, 0, 3],
    [0, 4, 0]
])
# Calling function
coord_dict = matrix_to_coordinate_dict(my_matrix)
print(coord_dict)

輸出

 {(0, 1): 1, (1, 0): 2, (1, 2): 3, (2, 1): 4}

結論

我們討論了在 Python 中將矩陣轉換為座標字典的各種方法。座標值以元組的形式表示,其中兩個不同的整數組合在一起。有一些應用程式,例如稀疏矩陣表示、矩陣操作和圖演算法。

更新於: 2023年8月16日

356 次檢視

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告