方法一:
https://blog.csdn.net/weixin_45609535/article/details/127136005
from collections import namedtuple
Triple = namedtuple("Triple", "row col value")
class TripleTable(object):
def __init__(self, table, row_num, col_num):
self.table = table
self.row_num = row_num
self.col_num = col_num
self.length = len(self.table)
def transposition(triple_table):
table_post = list()
row_num_post = triple_table.col_num
col_num_post = triple_table.row_num
table_pre = triple_table.table
for col in range(triple_table.col_num):
for tri_tuple in table_pre:
if tri_tuple.col == col:
tri_tuple_post = Triple(tri_tuple.col, tri_tuple.row, tri_tuple.value)
table_post.append(tri_tuple_post)
return TripleTable(table_post, row_num_post, col_num_post)
tri1 = Triple(1, 2, 12)
tri2 = Triple(1, 3, 9)
tri3 = Triple(3, 1, -3)
tri4 = Triple(3, 6, 14)
tri5 = Triple(4, 3, 24)
tri6 = Triple(5, 2, 18)
tri7 = Triple(6, 1, 15)
tri8 = Triple(6, 4, -7)
test_table = TripleTable([tri1, tri2, tri3, tri4, tri5, tri6, tri7, tri8], 6, 6)
print(transposition(test_table).table)
# transpose_TSMatrix.py
from collections import namedtuple, deque
from typing import List
from array import array
Triple = namedtuple("Triple", "row col value")
class TripletTable:
def __init__(self, triplet_list: List[Triple], row_num: int, col_num: int):
self.triplet_table = deque([None])
self.triplet_table.extend(triplet_list)
self.row_num = row_num
self.col_num = col_num
self.triplet_len = len(self.triplet_table) - 1
def transpose_ts_matrix(triplet_table: TripletTable):
"""
稀疏矩阵三元组转置
为了便于说明,此处行列下标都从1算起
:param triplet_table: 转置前的三元组对象
:return: 转置后的稀疏三元组对象
"""
if not triplet_table.triplet_len: # 当传进的三元组对象空时,返回-1
return -1
# 创建一个新的TripletTable对象,用于存放转置后的稀疏三元组对象
after_transpose = TripletTable([None]*(triplet_table.triplet_len+1), triplet_table.col_num, triplet_table.row_num)
after_transpose.triplet_len = triplet_table.triplet_len
before_col_ele_num = array('i', [0]*(triplet_table.col_num+1)) # 转置前的每列的元素个数,也是转置后的每行的元素个数
after_first_pos = array('i', [0]*(triplet_table.row_num+1)) #
for i in range(1, triplet_table.triplet_len+1): # 采用数组下标计数法计算每一列的非零元素的个数
before_col_ele_num[triplet_table.triplet_table[i].col] += 1
after_first_pos[1] = 1
for i in range(2, after_transpose.col_num+1): # 求i列中第一个非零元素在after_transpose。triplet_table中的正确位置
after_first_pos[i] = after_first_pos[i-1] + before_col_ele_num[i-1]
for i in range(1, triplet_table.triplet_len+1): # 将被转置矩阵的三元组表从头至尾扫描一次,实现矩阵转置
col = triplet_table.triplet_table[i].col
q = after_first_pos[col]
after_transpose.triplet_table[q] = Triple(triplet_table.triplet_table[i].col,
triplet_table.triplet_table[i].row, triplet_table.triplet_table[i].value)
after_first_pos[col] += 1 # position[col]加1,指向下一个列号为col的非零元素在三元组表after_transpose中的存放位置
return after_transpose # 转置后的稀疏三元组对象
# test_transpose_TSMatrix.py
from transpose_TSMatrix import Triple, TripletTable, transpose_ts_matrix
tri1 = Triple(1, 2, 12)
tri2 = Triple(1, 3, 9)
tri3 = Triple(3, 1, -3)
tri4 = Triple(3, 6, 14)
tri5 = Triple(4, 3, 24)
tri6 = Triple(5, 2, 18)
tri7 = Triple(6, 1, 15)
tri8 = Triple(6, 4, -7)
triplet_table = TripletTable([tri1, tri2, tri3, tri4, tri5, tri6, tri7, tri8], 6, 6)
triplet_table = transpose_ts_matrix(triplet_table)
for triple in triplet_table.triplet_table:
if triple:
print(triple)
"""
运行结果:
Triple(row=1, col=3, value=-3)
Triple(row=1, col=6, value=15)
Triple(row=2, col=1, value=12)
Triple(row=2, col=5, value=18)
Triple(row=3, col=1, value=9)
Triple(row=3, col=4, value=24)
Triple(row=4, col=6, value=-7)
Triple(row=6, col=3, value=14)
Process finished with exit code 0
"""
[引用《数据结构–用c语言描述 耿国华版》]