擅长:python、mysql、java
<p>这可能不是解决这个问题的最佳方法,但请看以下内容。。。你知道吗</p>
<pre><code>In [115]: b
Out[115]:
array([ True, True, True, False, False, True, True, False, False,
False, True, False, True], dtype=bool)
In [116]: l = [(k,len(list(g))) for k, g in itertools.groupby(b)]
In [117]: l
Out[117]:
[(True, 3),
(False, 2),
(True, 2),
(False, 3),
(True, 1),
(False, 1),
(True, 1)]
In [118]: l2 = [(True, x[1]) if x[1] in [1,2] else x for x in l]
In [119]: l2
Out[119]: [(True, 3), (True, 2), (True, 2), (False, 3), (True, 1), (True, 1), (True, 1)]
In [120]: l3 = [[x[0]] * x[1] for x in l2]
In [121]: l3
Out[121]:
[[True, True, True],
[True, True],
[True, True],
[False, False, False],
[True],
[True],
[True]]
In [122]: l4 = [x for x in itertools.chain(*l3)]
In [123]: l4
Out[123]:
[True,
True,
True,
True,
True,
True,
True,
False,
False,
False,
True,
True,
True]
In [124]: out = np.array(l4)
In [125]: out
Out[125]:
array([ True, True, True, True, True, True, True, False, False,
False, True, True, True], dtype=bool)
</code></pre>