<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: updateCursor row[n+1] from searchCursor row[n]-row[n+1] in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/updatecursor-row-n-1-from-searchcursor-row-n-row-n/m-p/1024674#M59862</link>
    <description>&lt;P&gt;very promising.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;...and simple.&amp;nbsp; Love that.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Let me give it a go and check back but def looks preferable to nested cursors.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;j&lt;/P&gt;</description>
    <pubDate>Mon, 08 Feb 2021 22:50:58 GMT</pubDate>
    <dc:creator>JamesCrandall</dc:creator>
    <dc:date>2021-02-08T22:50:58Z</dc:date>
    <item>
      <title>updateCursor row[n+1] from searchCursor row[n]-row[n+1]</title>
      <link>https://community.esri.com/t5/python-questions/updatecursor-row-n-1-from-searchcursor-row-n-row-n/m-p/1024581#M59851</link>
      <description>&lt;P&gt;FileGDB&lt;BR /&gt;ArcGIS 10.4&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Need to calculate elapsedSeconds column by first sorting on location_timestamp and then evaluate if PEP_land_name matches in row[n] and row[n+1]. If true, then set row[n+1] to elapsedSeconds value.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;fields = ['PEP_land_name','location_timestamp','elapsedSeconds']
with arcpy.da.UpdateCursor(tracksToProcMonth, fields, sql, sql_clause=(None, 'ORDER BY location_timesatmp ASC')) as u_cur:
	with arcpy.da.SearchCursor(tracksToProcMonth,fields, sql, sql_clause=(None, 'ORDER BY location_timestamp ASC')) as cursor:
	  for row in cursor:
		firstlLand = row[0]
		firstTimestamp = row[1]
		try:
		  nextLand = cursor.next()[0]
		  nextTimestamp = cursor.next()[1]

		  if firstlLand == nextLand:
			tdt = nextTimestamp - firstTimestamp
			totSecs = tdt.total_seconds()
			print 'totSecs: {}'.format(totSecs)
			u_row = totSecs
			u_cur.updateRow()

		  else:
			totSecs = 0.00
			u_row = totSecs
			u_cur.updateRow()&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="2021-02-08_14-56-30.jpg" style="width: 558px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/5549iC1F2A36595BF51B7/image-size/large?v=v2&amp;amp;px=999" role="button" title="2021-02-08_14-56-30.jpg" alt="2021-02-08_14-56-30.jpg" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I seem to be close in that I can arrive at the correct sort and evaluation, but I'm not exactly sure what to do about the updateCursor and setting the elapsedSeconds value in row[n+1].&amp;nbsp; In the attached image, OID 1 elapsedSeconds would be 0, OID 5 would be 5, and so on and on...&lt;/P&gt;</description>
      <pubDate>Mon, 08 Feb 2021 20:18:29 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/updatecursor-row-n-1-from-searchcursor-row-n-row-n/m-p/1024581#M59851</guid>
      <dc:creator>JamesCrandall</dc:creator>
      <dc:date>2021-02-08T20:18:29Z</dc:date>
    </item>
    <item>
      <title>Re: updateCursor row[n+1] from searchCursor row[n]-row[n+1]</title>
      <link>https://community.esri.com/t5/python-questions/updatecursor-row-n-1-from-searchcursor-row-n-row-n/m-p/1024610#M59853</link>
      <description>&lt;P&gt;u_cur.updateRow(row)&amp;nbsp; Might be what you are after?&lt;/P&gt;&lt;P&gt;... You can move it out from the if/else conditionals because you are setting u_row either way.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;        if firstlLand == nextLand:
            tdt = nextTimestamp - firstTimestamp
            totSecs = tdt.total_seconds()
            print 'totSecs: {}'.format(totSecs)
            u_row = totSecs

        else:
            totSecs = 0.00
            u_row = totSecs

        u_cur.updateRow(u_row)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 08 Feb 2021 20:52:25 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/updatecursor-row-n-1-from-searchcursor-row-n-row-n/m-p/1024610#M59853</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2021-02-08T20:52:25Z</dc:date>
    </item>
    <item>
      <title>Re: updateCursor row[n+1] from searchCursor row[n]-row[n+1]</title>
      <link>https://community.esri.com/t5/python-questions/updatecursor-row-n-1-from-searchcursor-row-n-row-n/m-p/1024612#M59854</link>
      <description>&lt;P&gt;if the rows oid=1 and oid=5 (see image in op) are the 2 rows being evaluated and elapsedSeconds calculated, does your suggestion update row OID=5?&lt;/P&gt;&lt;P&gt;In the image OID=1 should be 0.00 seconds and OID=5 should be 5.00 seconds&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Mon, 08 Feb 2021 21:00:16 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/updatecursor-row-n-1-from-searchcursor-row-n-row-n/m-p/1024612#M59854</guid>
      <dc:creator>JamesCrandall</dc:creator>
      <dc:date>2021-02-08T21:00:16Z</dc:date>
    </item>
    <item>
      <title>Re: updateCursor row[n+1] from searchCursor row[n]-row[n+1]</title>
      <link>https://community.esri.com/t5/python-questions/updatecursor-row-n-1-from-searchcursor-row-n-row-n/m-p/1024627#M59856</link>
      <description>&lt;P&gt;The first thing I saw from your code sample was u_row missing from the .updateRow().&amp;nbsp; I can't really answer your question without running it through the debugger and verifying that the rest of your code is valid. I can say that it will update whichever row 'u_row' is currently on.&lt;/P&gt;</description>
      <pubDate>Mon, 08 Feb 2021 21:25:52 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/updatecursor-row-n-1-from-searchcursor-row-n-row-n/m-p/1024627#M59856</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2021-02-08T21:25:52Z</dc:date>
    </item>
    <item>
      <title>Re: updateCursor row[n+1] from searchCursor row[n]-row[n+1]</title>
      <link>https://community.esri.com/t5/python-questions/updatecursor-row-n-1-from-searchcursor-row-n-row-n/m-p/1024634#M59858</link>
      <description>&lt;P&gt;right... I think that's my problem: I cannot update row[n+1] rather than the current row.&lt;/P&gt;</description>
      <pubDate>Mon, 08 Feb 2021 21:35:49 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/updatecursor-row-n-1-from-searchcursor-row-n-row-n/m-p/1024634#M59858</guid>
      <dc:creator>JamesCrandall</dc:creator>
      <dc:date>2021-02-08T21:35:49Z</dc:date>
    </item>
    <item>
      <title>Re: updateCursor row[n+1] from searchCursor row[n]-row[n+1]</title>
      <link>https://community.esri.com/t5/python-questions/updatecursor-row-n-1-from-searchcursor-row-n-row-n/m-p/1024650#M59859</link>
      <description>&lt;P&gt;You could probably work the values into a dictionary and do the processing outside of the cursors and then come back to update the values by OID.&amp;nbsp; I can work up a sample latter today if that is feasible.&lt;/P&gt;</description>
      <pubDate>Mon, 08 Feb 2021 21:54:12 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/updatecursor-row-n-1-from-searchcursor-row-n-row-n/m-p/1024650#M59859</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2021-02-08T21:54:12Z</dc:date>
    </item>
    <item>
      <title>Re: updateCursor row[n+1] from searchCursor row[n]-row[n+1]</title>
      <link>https://community.esri.com/t5/python-questions/updatecursor-row-n-1-from-searchcursor-row-n-row-n/m-p/1024661#M59860</link>
      <description>&lt;P&gt;I'm also incorrectly implementing cursor.next()&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It seems each time that is invoked it immediately skips to the next row.&amp;nbsp; I'm not exactly sure how best to get the values for row n and n+1 WHILE the current row is at n.&lt;/P&gt;</description>
      <pubDate>Mon, 08 Feb 2021 22:16:06 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/updatecursor-row-n-1-from-searchcursor-row-n-row-n/m-p/1024661#M59860</guid>
      <dc:creator>JamesCrandall</dc:creator>
      <dc:date>2021-02-08T22:16:06Z</dc:date>
    </item>
    <item>
      <title>Re: updateCursor row[n+1] from searchCursor row[n]-row[n+1]</title>
      <link>https://community.esri.com/t5/python-questions/updatecursor-row-n-1-from-searchcursor-row-n-row-n/m-p/1024664#M59861</link>
      <description>&lt;P&gt;Unless I've misinterpreted what you're trying to achieve, I think it can be done without and .next() or nested cursors.&lt;/P&gt;&lt;P&gt;I've just created global variables to hold the previous land and timestamp values for each row:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy


fc = r'PathtoMyFC'
previous_timestamp = ''
previous_land = ''

with arcpy.da.UpdateCursor(fc, ['landname', 'timestamp', 'elapsed']) as cursor:
    for row in cursor:
   
        this_timestamp = row[1]
        current_land = row[0]

        if previous_timestamp is not None and current_land == previous_land:
            time_diff = this_timestamp - previous_timestamp
            total_seconds = time_diff.total_seconds()
            row[2] = total_seconds

        previous_timestamp = this_timestamp
        previous_land = current_land
        cursor.updateRow(row)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="updateCursorTime.png" style="width: 616px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/5567iA63D1642A87DEC66/image-size/large?v=v2&amp;amp;px=999" role="button" title="updateCursorTime.png" alt="updateCursorTime.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 08 Feb 2021 23:20:26 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/updatecursor-row-n-1-from-searchcursor-row-n-row-n/m-p/1024664#M59861</guid>
      <dc:creator>DavidPike</dc:creator>
      <dc:date>2021-02-08T23:20:26Z</dc:date>
    </item>
    <item>
      <title>Re: updateCursor row[n+1] from searchCursor row[n]-row[n+1]</title>
      <link>https://community.esri.com/t5/python-questions/updatecursor-row-n-1-from-searchcursor-row-n-row-n/m-p/1024674#M59862</link>
      <description>&lt;P&gt;very promising.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;...and simple.&amp;nbsp; Love that.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Let me give it a go and check back but def looks preferable to nested cursors.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;j&lt;/P&gt;</description>
      <pubDate>Mon, 08 Feb 2021 22:50:58 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/updatecursor-row-n-1-from-searchcursor-row-n-row-n/m-p/1024674#M59862</guid>
      <dc:creator>JamesCrandall</dc:creator>
      <dc:date>2021-02-08T22:50:58Z</dc:date>
    </item>
    <item>
      <title>Re: updateCursor row[n+1] from searchCursor row[n]-row[n+1]</title>
      <link>https://community.esri.com/t5/python-questions/updatecursor-row-n-1-from-searchcursor-row-n-row-n/m-p/1024702#M59863</link>
      <description>&lt;P&gt;I will go ahead and mark as the solution because it appears to work as intended and I am running into another issue that needs resolved in order to actually implement this.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks again!&lt;/P&gt;</description>
      <pubDate>Tue, 09 Feb 2021 00:44:18 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/updatecursor-row-n-1-from-searchcursor-row-n-row-n/m-p/1024702#M59863</guid>
      <dc:creator>JamesCrandall</dc:creator>
      <dc:date>2021-02-09T00:44:18Z</dc:date>
    </item>
    <item>
      <title>Re: updateCursor row[n+1] from searchCursor row[n]-row[n+1]</title>
      <link>https://community.esri.com/t5/python-questions/updatecursor-row-n-1-from-searchcursor-row-n-row-n/m-p/1024926#M59888</link>
      <description>&lt;P&gt;From a semantics perspective, this is a n-1 solution and not n+1.&amp;nbsp; It is not possible to do an n+1 with a single cursor or a single loop through a cursor.&lt;/P&gt;</description>
      <pubDate>Tue, 09 Feb 2021 16:27:01 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/updatecursor-row-n-1-from-searchcursor-row-n-row-n/m-p/1024926#M59888</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2021-02-09T16:27:01Z</dc:date>
    </item>
  </channel>
</rss>

