I am trying to compare two values, and can't seem to access the next row in a cursor.
for i, row in enumerate(cursor):
if row[0] == cursor.next()[0]
Any ideas?
See this thread for ideas: Compare value from n and n+1 row in searchCursor
Beyond reading through the thread recommended by Randy Burton, a couple of questions:
UPDATE: continued....
As mentioned in the referenced GeoNet discussion, ArcPy cursors do not implement any kind of look- or read-ahead feature. If one is working solely within DBMSs, there are likely set-based approaches that are better then using cursors for look-ahead functionality, .e.,g LAG and LEAD functionality implemented in many DBMSs; but one needs to work with what is available to them.
There are multiple ways to emulate look- or read-ahead functionality with Python and ArcPy, several are covered in the aforementioned GeoNet discussion. Since I commented on that discussion last year, I have refined the approach I put forward. Functionally, the approach below is not any different, but syntactically it is much more streamlined, I would argue more Pythonic.
<SPAN class="keyword token">from</SPAN> itertools <SPAN class="keyword token">import</SPAN> izip_longest fc <SPAN class="operator token">=</SPAN> <SPAN class="comment token"># path to feature class</SPAN> flds <SPAN class="operator token">=</SPAN> <SPAN class="comment token"># string list of field names for cursors</SPAN> <SPAN class="keyword token">with</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>da<SPAN class="punctuation token">.</SPAN>SearchCursor<SPAN class="punctuation token">(</SPAN>fc<SPAN class="punctuation token">,</SPAN> flds<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">as</SPAN> n1_cur<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> flds<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">as</SPAN> n_cur<SPAN class="punctuation token">:</SPAN> filler_row <SPAN class="operator token">=</SPAN> next<SPAN class="punctuation token">(</SPAN>n1_cur<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">for</SPAN> n_row<SPAN class="punctuation token">,</SPAN> n1_row <SPAN class="keyword token">in</SPAN> izip_longest<SPAN class="punctuation token">(</SPAN>n_cur<SPAN class="punctuation token">,</SPAN> n1_cur<SPAN class="punctuation token">,</SPAN> fillvalue<SPAN class="operator token">=</SPAN>filler_row<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">if</SPAN> n_row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">==</SPAN> n1_row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">:</SPAN> n_cur<SPAN class="punctuation token">.</SPAN>updateRow<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><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>
The basic approach is to pair a search cursor with an update cursor. The search cursor is used for the look ahead (n+1 cursor or n1_cur) while the update cursor is used for the current record (n cursor or n_cur). The itertools.izip_longest() function is used to pair the two cursors so they can be stepped through together, with the search cursor being one record ahead of the update cursor.
UPDATE 2: Removed empty_row and created filler_row since previous code for empty_row would fail with datetime or geometry fields.
Membros conectados podem postar, seguir atualizações e mais. Novo aqui? Registre uma conta gratuita.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.