1 条题解
-
0
NUMPY059 题解:按指定列排序
题目
按第 k 列对矩阵排序。
解题思路
- 获取第 k 列
- 用 \texttt{argsort} 得到排序索引
- 用索引重新排列矩阵
代码
import numpy as np m, n, k = map(int, input().split()) Z = np.array([list(map(int, input().split())) for _ in range(m)]) sorted_idx = Z[:, k].argsort() print(Z[sorted_idx])代码详解
- \texttt{Z[:, k]} —— 选取第 k 列
- \texttt{.argsort()} —— 返回排序索引
- \texttt{Z[sorted_idx]} —— 按索引重新排列
核心知识点
- \texttt{argsort} —— 返回排序索引
- 数组索引
- 1
信息
- ID
- 312
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- (无)
- 标签
- (无)
- 递交数
- 0
- 已通过
- 0
- 上传者