
Hi there. I need help assembling a python script that counts the number of consecutive numbers in a Field (Long Int) and writes that count in another field for those numbers. The field comes sorted appropriately.The image is what I am trying to accomplish. I cobbled together an Update Cursor, but having a hard time integrating the counting code into it. Any help is much appreciated.
<SPAN class="keyword token">import</SPAN> arcpy
fc <SPAN class="operator token">=</SPAN> r<SPAN class="string token">'C:\Users\a19573\Desktop\Test\Test.gdb\TestCount'</SPAN>
fields <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="string token">'Consecutive'</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">'Count'</SPAN><SPAN class="punctuation token">]</SPAN>
count_values <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">{</SPAN><SPAN class="punctuation token">}</SPAN>
<SPAN class="keyword token">with</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>da<SPAN class="punctuation token">.</SPAN>UpdateCursor <SPAN class="punctuation token">(</SPAN>fc<SPAN class="punctuation token">,</SPAN> fields<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">as</SPAN> cursor<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">for</SPAN> row <SPAN class="keyword token">in</SPAN> cursor<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">if</SPAN> <SPAN class="operator token">not</SPAN> row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="keyword token">in</SPAN> count_values<SPAN class="punctuation token">.</SPAN>keys<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
count_values<SPAN class="punctuation token">[</SPAN>row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">=</SPAN> <SPAN class="number token">1</SPAN>
<SPAN class="keyword token">else</SPAN><SPAN class="punctuation token">:</SPAN>
count_values<SPAN class="punctuation token">[</SPAN>row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">+=</SPAN> <SPAN class="number token">1</SPAN>
row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">=</SPAN> count_values<SPAN class="punctuation token">[</SPAN>row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">]</SPAN>
cursor<SPAN class="punctuation token">.</SPAN>updateRow<SPAN class="punctuation token">(</SPAN>row<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">### Test Code to integrate into Update Cursor above</SPAN>
random_list<SPAN class="operator token">=</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="number token">4</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="number token">5</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="number token">6</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="number token">7</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="number token">9</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="number token">9</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="number token">19</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="number token">21</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="number token">22</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="number token">23</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="number token">24</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="comment token"># Set of consecutive numbers in Field 'Consecutive'</SPAN>
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">count_consec</SPAN><SPAN class="punctuation token">(</SPAN>lst<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
consec <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="keyword token">for</SPAN> x<SPAN class="punctuation token">,</SPAN> y <SPAN class="keyword token">in</SPAN> zip<SPAN class="punctuation token">(</SPAN>lst<SPAN class="punctuation token">,</SPAN> lst<SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">:</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">if</SPAN> x <SPAN class="operator token">==</SPAN> y <SPAN class="operator token">-</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">:</SPAN>
consec<SPAN class="punctuation token">[</SPAN><SPAN class="operator token">-</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">+=</SPAN> <SPAN class="number token">1</SPAN>
<SPAN class="keyword token">else</SPAN><SPAN class="punctuation token">:</SPAN>
consec<SPAN class="punctuation token">.</SPAN>append<SPAN class="punctuation token">(</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">return</SPAN> consec
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>count_consec<SPAN class="punctuation token">(</SPAN>random_list<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>