python矩阵的转置和逆转实例

yipeiwu_com6年前Python基础

如下所示:

# 矩阵的转置
def transpose(list1):
 return [list(row) for row in zip(*list1)]
 
list1 = [[1, 4], [2, 5], [3, 6]]
print(transpose(list1)) # [[1, 2, 3], [4, 5, 6]]

矩阵转置

用zip将一系列可迭代对象中的元素打包为元组,之后将这些元组放置在列表中,两步加起来等价于行列转置。

# 矩阵逆转
def invert(list1):
 return [row[::-1] for row in list1]
list1 = [[1, 4], [2, 5], [3, 6]]
print(invert(list1)) # [[4, 1], [5, 2], [6, 3]]

矩阵逆转

取出每行的元素,逆序索引遍历 = 左右翻转。

以上这篇python矩阵的转置和逆转实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python使用shelve模块实现简单数据存储的方法

本文实例讲述了Python使用shelve模块实现简单数据存储的方法。分享给大家供大家参考。具体分析如下: Python的shelve模块提供了一种简单的数据存储方案,以dict(字典)...

Pytorch实现神经网络的分类方式

本文用于利用Pytorch实现神经网络的分类!!! 1.训练神经网络分类模型 import torch from torch.autograd import Variable imp...

解决win7操作系统Python3.7.1安装后启动提示缺少.dll文件问题

解决win7操作系统Python3.7.1安装后启动提示缺少.dll文件问题

错误提示图片 首先,我的操作系统是win7旗舰版,安装Python3.7.1之后启动时,提示如图错误,网上比较多的是两种处理方法: (1)安装Windows补丁程序 (2)安装...

Python3之读取连接过的网络并定位的方法

如下所示: #!/usr/bin/python # coding=utf-8 import json from urllib.request import urlopen from...

python多线程扫描端口示例

复制代码 代码如下:# -*- coding: cp936 -*-import socketfrom threading import Thread,activeCount,Lockfr...