小编lez*_*zaf的帖子

是否有更好的方法来删除长度等于或大于阈值的连续零部分?

问题陈述:

\n

如标题所述,我想从一维数组中删除具有连续 长度等于或大于的部分。

\n
\n

我的解决方案:

\n

我生成了以下 MRE 中所示的解决方案:

\n
import numpy as np\n\nTHRESHOLD = 4\n\na = np.array((1,1,0,1,0,0,0,0,1,1,0,0,0,1,0,0,0,0,0,1))\n\nprint("Input: " + str(a))\n\n# Find the indices of the parts that meet threshold requirement\ngaps_above_threshold_inds = np.where(np.diff(np.nonzero(a)[0]) - 1 >= THRESHOLD)[0]\n\n# Delete these parts from array\nfor idx in gaps_above_threshold_inds:\n    a = np.delete(a, list(range(np.nonzero(a)[0][idx] + 1, np.nonzero(a)[0][idx + 1])))\n    \nprint("Output: " + str(a))\n
Run Code Online (Sandbox Code Playgroud)\n

输出:

\n
Input:  [1 1 0 1 0 0 0 0 1 1 …
Run Code Online (Sandbox Code Playgroud)

python performance signal-processing numpy

3
推荐指数
1
解决办法
491
查看次数

标签 统计

numpy ×1

performance ×1

python ×1

signal-processing ×1