如标题所述,我想从一维数组中删除具有连续 零且长度等于或大于阈值的部分。
\n我生成了以下 MRE 中所示的解决方案:
\nimport 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))\nRun Code Online (Sandbox Code Playgroud)\n输出:
\nInput: [1 1 0 1 0 0 0 0 1 1 …Run Code Online (Sandbox Code Playgroud)