numpy.ndarray 交换多维数组(矩阵)的行/列
2016-06-22 22:30
453 查看
>> import numpy as np >> P = np.eye(3) >> P array([[ 1., 0., 0.], [ 0., 1., 0.], [ 0., 0., 1.]])
交换第 0 行和第 2 行:
>> P[[0, 2], :] = P[[2, 0], :] # P[(0, 2), :] = P[(2, 0), :] >> P array([[ 0., 0., 1.], [ 0., 1., 0.], [ 1., 0., 0.]])
再交换第一列和第三列:
>> P[:, [0, 2]] = P[:, [2, 0]] >> P array([[ 1., 0., 0.], [ 0., 1., 0.], [ 0., 0., 1.]])
注意以下是错误的做法:
>> P[0, :], P[2, :] = P[2, :], P[0, :] >> P array([[ 0., 0., 1.], [ 0., 1., 0.], [ 0., 0., 1.]]) # 不尽写出来啰嗦,而且代表的意义也不是交换
相关文章推荐
- Pearson相关系数公式的四种形式及Python代码实现
- [Python] from scipy import sparse 报 DLL load failed:找不到指定模块错误
- numpy中矩阵转置
- python: argparse模块
- Python Curses
- python yield send 一个容易犯错的地方
- Introduction of Python package: MySQL Connector
- Python中urlopen()介绍
- OpenCV在Python上的调用( import cv2的解决办法)
- python logging - 高级
- python学习之 字符串前'r'的用法
- Python正则表达式
- Python 描述符(descriptor) 杂记
- python mysql
- python之路基础篇
- Python Django 之 模板
- 终于解决!!RF切换工作目录,调用其他目录中的python函数,搞屎我了
- Python数据预处理:缺失值替换
- Python 使用dpkt分析数据包
- python之数据库操作(sqlite)