2024-04-19 09:21:06 发布
网友
我有一个大小为3×7的二维数组,numpy:
[[1 2 3 4 5 6 7] [4 5 6 7 8 9 0] [2 3 4 5 6 7 8]]
我还有一个包含拆分点索引的列表:
[1, 3]
现在,我想使用列表中的索引拆分数组,以便得到:
[[1 2] [4 5] [2 3]] [[ 2 3 4] [5 6 7] [3 4 5]] [[ 4 5 6 7] [7 8 9 0] [5 6 7 8]]
如何在python中执行此操作?你知道吗
您可以使用带有切片的列表理解,使用zip成对地提取索引。你知道吗
zip
A = np.array([[1, 2, 3, 4, 5, 6, 7], [4, 5, 6, 7, 8, 9, 0], [2, 3, 4, 5, 6, 7, 8]]) idx = [1, 3] idx = [0] + idx + [A.shape[1]] res = [A[:, start: end+1] for start, end in zip(idx, idx[1:])] print(*res, sep='\n'*2) [[1 2] [4 5] [2 3]] [[2 3 4] [5 6 7] [3 4 5]] [[4 5 6 7] [7 8 9 0] [5 6 7 8]]
您可以使用带有切片的列表理解,使用
zip
成对地提取索引。你知道吗相关问题 更多 >
编程相关推荐