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 次查看

开启你的 职业生涯

通过完成课程获得认证

开始学习
广告

© . All rights reserved.