擅长:python、mysql、java
<p>下面是一种使用内置<code>array</code>模块的方法。通过将<code>a</code>行表示为Python数组,我们可以直接对表示每行的字节调用<code>replace</code>。结果与预期的输出不完全匹配(我不确定示例输出的第2行和第3行中为什么有前导零)</p>
<pre><code>a =[[0,0,0,0,0,0,0],
[0,1,1,1,1,1,0],
[0,1,1,1,1,1,0],
[0,0,0,0,0,0,0],]
import array
def detectfunction(a, pattern, subst):
pattern_bytes = array.array('d', pattern).tobytes()
subst_bytes = array.array('d', subst).tobytes()
result = list()
for row in a:
arr_row = array.array('d', row)
new_arr_row = array.array('d', [])
new_arr_row.frombytes(arr_row.tobytes().replace(pattern_bytes, subst_bytes))
result.append(new_arr_row.tolist())
return result
result = detectfunction(a,[0,1,1],[0.25,0.5,0.75])
# [[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
# [0.25, 0.5, 0.75, 1.0, 1.0, 1.0, 0.0],
# [0.25, 0.5, 0.75, 1.0, 1.0, 1.0, 0.0],
# [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]]
</code></pre>