python - Swapping columns of a matrix without numpy
问题描述
I need to swap the columns of a given matrix by applying some function to (M,i,j)
to swap the i
th and j
th columns of M
.
def swap_columns(M,i,j)
rows = M[i]
col = M[i][j]
for i in M:
N = M[i][j]
M[i][j] = M[j][i]
M[j][i] = N
return N
Unable to get this working at all.
解决方案
In python
variable swapping can be done by: x, y = y, x
Code:
This function will modify the original matrix. No need to return
def col_swapper(matrix, col_1, col_2):
for line in range(len(matrix)):
matrix[line][col_1], matrix[line][col_2] = matrix[line][col_2], matrix[line][col_1]
推荐阅读
- python - 跨同一数据帧的多个滚动平均值
- javascript - 如何用 JavaScript 编写简单的“数字阶梯”?
- python - Pickle EOFError:输入不足
- amazon-web-services - S3ToRedshiftTransfert 在创建的表中返回 0 元素
- azure - WebSocket 握手期间出错:意外的响应代码:在 azure 上与 ocelot 网关连接时出现 502
- reactjs - 为什么要在 url %E2%80%8B 中获取 api 添加转义字符
- sql - 如何从 SQL Server 表中提取第二高的产品使用率?
- r - 数据序列 - 如何提取与时间序列中的时间相关的特定数据
- java - Java 使用 Stream 按值对映射进行排序
- swift - 我可以在 ARView 中使用缩放功能吗?