首页 > 解决方案 > 在python中添加多个切片

问题描述

我想在单个数组中获取x[start1:stop1:step1 ]and 的切片。x[start2:stop2:step2]

Python中是否有这样的语法:

x[start1:stop1:step1  +  start2:stop2:step2]

在 Matlab 中很简单:

x(start1:step1:stop1  start2:step2:stop2)


更新:

我看到很多答案都建议串联。

我最初的目标是为更高维数组做切片,例如在 2D 中:

x[start1:stop1 +  start2:stop2,  start3:stop3  +  start4:stop4]

串联没问题。但是在高维度上它似乎太复杂了。

有没有像matlab一样简单的方法?

x(start1:stop1  start2:stop2, start3:stop3  start4:stop4 )

我们可以连接数组索引,而不是数组本身吗?

请参阅下面的解决方案

格子呢

标签: pythonslice

解决方案


您可以像这样连接两个数组(列表):x[start1:stop1:step1] + x[start2:stop2:step2]


推荐阅读