擅长:python、mysql、java
<p>有一个简单的方法。<br/>
假设你的列表如下:A=[1,2,<strong>6,8,7,6,8</strong>,3,2,3,<strong>6,10,6,7,8</strong>,2,1,0,2]<br/>
你想知道有多少连续序列的值大于<strong>6</strong>且长度为5。例如,这里你的答案是2。有两个序列的值大于6,且序列的长度为5。在python和pandas中,我们如下所示:<br/></p>
<pre><code> condition = (df.wanted_row > 6) & \
(df.wanted_row.shift(-1) > 6) & \
(df.wanted_row.shift(-2) > 6) & \
(df.wanted_row.shift(-3) > 6) & \
(df.wanted_row.shift(-4) > 6)
consecutive_count = df[condition].count().head(1)[0]
</code></pre>