<?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: Compare value from n and n+1 row in searchCursor in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/compare-value-from-n-and-n-1-row-in-searchcursor/m-p/224888#M17367</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You could create a counter parameter at the beginning of the script:&lt;/P&gt;&lt;P&gt;counter = 0&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Then enter the For loop.&amp;nbsp; At the bottom of the loop, increase the counter.&amp;nbsp; If the counter is equal to 0 then don't compare.&amp;nbsp; If the counter is greater than 0, compare the values:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;SPAN style="color: black; font-weight: inherit; font-size: 9pt !important; font-family: arial, helvetica, sans-serif; font-style: inherit;"&gt;column = "name"
&lt;/SPAN&gt;
&lt;SPAN style="color: black; font-weight: inherit; font-size: 9pt !important; font-family: arial, helvetica, sans-serif; font-style: inherit;"&gt;cursor = arcpy.SearchCursor(table)&lt;/SPAN&gt;
&lt;SPAN style="color: black; font-weight: inherit; font-size: 9pt !important; font-family: arial, helvetica, sans-serif; font-style: inherit;"&gt;counter = 0&lt;/SPAN&gt;
&lt;SPAN style="color: black; font-weight: inherit; font-size: 9pt !important; font-family: arial, helvetica, sans-serif; font-style: inherit;"&gt;for row in cursor:&lt;/SPAN&gt;
&lt;SPAN style="color: black; font-weight: inherit; font-size: 9pt !important; font-family: arial, helvetica, sans-serif; font-style: inherit;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; n =&lt;/SPAN&gt;&lt;SPAN style="color: black; font-weight: inherit; font-size: 9pt !important; font-family: arial, helvetica, sans-serif; font-style: inherit;"&gt; row.getValue(column)&lt;/SPAN&gt;
&lt;SPAN style="color: black; font-weight: inherit; font-size: 9pt !important; font-family: arial, helvetica, sans-serif; font-style: inherit;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if counter &amp;gt; 0:&lt;/SPAN&gt;
&lt;SPAN style="color: black; font-weight: inherit; font-size: 9pt !important; font-family: arial, helvetica, sans-serif; font-style: inherit;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ### Compare n and oldN in here&lt;/SPAN&gt;
&lt;SPAN style="color: black; font-weight: inherit; font-size: 9pt !important; font-family: arial, helvetica, sans-serif; font-style: inherit;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; oldN = n&lt;/SPAN&gt;
&lt;SPAN style="color: black; font-weight: inherit; font-size: 9pt !important; font-family: arial, helvetica, sans-serif; font-style: inherit;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; counter = counter + 1&lt;/SPAN&gt;
&lt;SPAN style="color: black; font-weight: inherit; font-size: 9pt !important; font-family: arial, helvetica, sans-serif; font-style: inherit;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print(row.getValue(column))&lt;/SPAN&gt;
&lt;/PRE&gt;&lt;P&gt;&lt;SPAN style="color: black; font-weight: inherit; font-size: 9pt !important; font-style: inherit;"&gt; &lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 10:57:25 GMT</pubDate>
    <dc:creator>JenniferMcCall4</dc:creator>
    <dc:date>2021-12-11T10:57:25Z</dc:date>
    <item>
      <title>Compare value from n and n+1 row in searchCursor</title>
      <link>https://community.esri.com/t5/python-questions/compare-value-from-n-and-n-1-row-in-searchcursor/m-p/224885#M17364</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P style="font-size: 14px; font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif; color: #3d3d3d;"&gt;Hello,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P style="font-size: 14px; font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif; color: #3d3d3d;"&gt;I have a searchCursor that goes through a specific column in a table. On every row I need to compare the value from this row with the value on the next row and if they are equal do something and if they aren't equal do something else.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P class="dp-highlighter" style="color: #3d3d3d;"&gt;&lt;/P&gt;&lt;DIV class="bar" style="font-weight: inherit; font-style: inherit; font-family: inherit;"&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="python" __jive_macro_name="code" class="jive_macro_code _jivemacro_uid_14696226742556356 jive_text_macro" data-renderedposition="189.390625_50_798_108" jivemacro_uid="_14696226742556356"&gt;&lt;OL class="dp-py" start="1" style="font-weight: inherit; font-style: inherit; font-family: inherit;"&gt;&lt;LI&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit;"&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit;"&gt;mxd = arcpy.mapping.MapDocument(&lt;/SPAN&gt;&lt;SPAN class="string" style="font-weight: inherit; font-style: inherit;"&gt;"CURRENT"&lt;/SPAN&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit;"&gt;)&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/LI&gt;&lt;LI&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit;"&gt;table = arcpy.mapping.ListTableViews(mxd)[&lt;SPAN class="number" style="font-weight: inherit; font-style: inherit;"&gt;0&lt;/SPAN&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit;"&gt;]&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/LI&gt;&lt;LI&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit;"&gt;column = &lt;SPAN class="string" style="font-weight: inherit; font-style: inherit;"&gt;"name"&lt;/SPAN&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit;"&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/LI&gt;&lt;LI&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit;"&gt;cursor=arcpy.SearchCursor(table)&amp;nbsp; &lt;/SPAN&gt;&lt;/LI&gt;&lt;LI&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit;"&gt;&lt;SPAN class="keyword" style="font-weight: inherit; font-style: inherit;"&gt;for&lt;/SPAN&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit;"&gt; row &lt;/SPAN&gt;&lt;SPAN class="keyword" style="font-weight: inherit; font-style: inherit;"&gt;in&lt;/SPAN&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit;"&gt; cursor:&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/LI&gt;&lt;LI&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class="keyword" style="font-weight: inherit; font-style: inherit;"&gt;print&lt;/SPAN&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit;"&gt;(row.getValue(column))&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/LI&gt;&lt;/OL&gt;&lt;/PRE&gt;&lt;/DIV&gt;&lt;P&gt;&lt;/P&gt;&lt;P style="font-size: 14px; font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif; color: #3d3d3d;"&gt;I have this sample of code. It goes through all rows. But how can I compare the value from row[row] with the value from row[row+1]??&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P style="font-size: 14px; font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif; color: #3d3d3d;"&gt;Thanks for any help.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 27 Jul 2016 12:32:58 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/compare-value-from-n-and-n-1-row-in-searchcursor/m-p/224885#M17364</guid>
      <dc:creator>ZdeněkSoldán</dc:creator>
      <dc:date>2016-07-27T12:32:58Z</dc:date>
    </item>
    <item>
      <title>Re: Compare value from n and n+1 row in searchCursor</title>
      <link>https://community.esri.com/t5/python-questions/compare-value-from-n-and-n-1-row-in-searchcursor/m-p/224886#M17365</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi&amp;nbsp; Zdeněk,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;At the end of the for statement, can you make a new parameter (called oldN or something) to store the value from the loop.&amp;nbsp; Then the next time the loop occurs compare n to oldN before the oldN parameter is reset at the end of the loop.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 27 Jul 2016 12:38:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/compare-value-from-n-and-n-1-row-in-searchcursor/m-p/224886#M17365</guid>
      <dc:creator>JenniferMcCall4</dc:creator>
      <dc:date>2016-07-27T12:38:02Z</dc:date>
    </item>
    <item>
      <title>Re: Compare value from n and n+1 row in searchCursor</title>
      <link>https://community.esri.com/t5/python-questions/compare-value-from-n-and-n-1-row-in-searchcursor/m-p/224887#M17366</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello Jennifer,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks for quick response. But I am not sure how to make first iterate step because I will not have what to compare.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 27 Jul 2016 12:45:20 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/compare-value-from-n-and-n-1-row-in-searchcursor/m-p/224887#M17366</guid>
      <dc:creator>ZdeněkSoldán</dc:creator>
      <dc:date>2016-07-27T12:45:20Z</dc:date>
    </item>
    <item>
      <title>Re: Compare value from n and n+1 row in searchCursor</title>
      <link>https://community.esri.com/t5/python-questions/compare-value-from-n-and-n-1-row-in-searchcursor/m-p/224888#M17367</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You could create a counter parameter at the beginning of the script:&lt;/P&gt;&lt;P&gt;counter = 0&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Then enter the For loop.&amp;nbsp; At the bottom of the loop, increase the counter.&amp;nbsp; If the counter is equal to 0 then don't compare.&amp;nbsp; If the counter is greater than 0, compare the values:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;SPAN style="color: black; font-weight: inherit; font-size: 9pt !important; font-family: arial, helvetica, sans-serif; font-style: inherit;"&gt;column = "name"
&lt;/SPAN&gt;
&lt;SPAN style="color: black; font-weight: inherit; font-size: 9pt !important; font-family: arial, helvetica, sans-serif; font-style: inherit;"&gt;cursor = arcpy.SearchCursor(table)&lt;/SPAN&gt;
&lt;SPAN style="color: black; font-weight: inherit; font-size: 9pt !important; font-family: arial, helvetica, sans-serif; font-style: inherit;"&gt;counter = 0&lt;/SPAN&gt;
&lt;SPAN style="color: black; font-weight: inherit; font-size: 9pt !important; font-family: arial, helvetica, sans-serif; font-style: inherit;"&gt;for row in cursor:&lt;/SPAN&gt;
&lt;SPAN style="color: black; font-weight: inherit; font-size: 9pt !important; font-family: arial, helvetica, sans-serif; font-style: inherit;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; n =&lt;/SPAN&gt;&lt;SPAN style="color: black; font-weight: inherit; font-size: 9pt !important; font-family: arial, helvetica, sans-serif; font-style: inherit;"&gt; row.getValue(column)&lt;/SPAN&gt;
&lt;SPAN style="color: black; font-weight: inherit; font-size: 9pt !important; font-family: arial, helvetica, sans-serif; font-style: inherit;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if counter &amp;gt; 0:&lt;/SPAN&gt;
&lt;SPAN style="color: black; font-weight: inherit; font-size: 9pt !important; font-family: arial, helvetica, sans-serif; font-style: inherit;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ### Compare n and oldN in here&lt;/SPAN&gt;
&lt;SPAN style="color: black; font-weight: inherit; font-size: 9pt !important; font-family: arial, helvetica, sans-serif; font-style: inherit;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; oldN = n&lt;/SPAN&gt;
&lt;SPAN style="color: black; font-weight: inherit; font-size: 9pt !important; font-family: arial, helvetica, sans-serif; font-style: inherit;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; counter = counter + 1&lt;/SPAN&gt;
&lt;SPAN style="color: black; font-weight: inherit; font-size: 9pt !important; font-family: arial, helvetica, sans-serif; font-style: inherit;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print(row.getValue(column))&lt;/SPAN&gt;
&lt;/PRE&gt;&lt;P&gt;&lt;SPAN style="color: black; font-weight: inherit; font-size: 9pt !important; font-style: inherit;"&gt; &lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 10:57:25 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/compare-value-from-n-and-n-1-row-in-searchcursor/m-p/224888#M17367</guid>
      <dc:creator>JenniferMcCall4</dc:creator>
      <dc:date>2021-12-11T10:57:25Z</dc:date>
    </item>
    <item>
      <title>Re: Compare value from n and n+1 row in searchCursor</title>
      <link>https://community.esri.com/t5/python-questions/compare-value-from-n-and-n-1-row-in-searchcursor/m-p/224889#M17368</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Just a few comments on the provided solution:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;If you have access to ArcGIS 10.1 or higher, use the cursors provided in the data access module for faster access&lt;/LI&gt;&lt;LI&gt;Cursors normally do not provide access to the values on the next 'row'. You can solve this by creating a list of values using list comprehensions before performing the update cursor&lt;/LI&gt;&lt;LI&gt;I added an additional field to write the resulting values to. There may be additional things to keep in mind when overwriting the existing values.&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;What I would do is this:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;def main():
&amp;nbsp;&amp;nbsp;&amp;nbsp; import arcpy

&amp;nbsp;&amp;nbsp;&amp;nbsp; # access data and define fields to use
&amp;nbsp;&amp;nbsp;&amp;nbsp; mxd = arcpy.mapping.MapDocument("CURRENT")
&amp;nbsp;&amp;nbsp;&amp;nbsp; table = arcpy.mapping.ListTableViews(mxd)[0]
&amp;nbsp;&amp;nbsp;&amp;nbsp; fld_in = "name"
&amp;nbsp;&amp;nbsp;&amp;nbsp; fld_out = "another field"&amp;nbsp; # I added a second field for output values

&amp;nbsp;&amp;nbsp;&amp;nbsp; # create a list of values from the field using list comprehensions
&amp;nbsp;&amp;nbsp;&amp;nbsp; lst_values = [r[0] for r in arcpy.da.SearchCursor(table, (fld))]

&amp;nbsp;&amp;nbsp;&amp;nbsp; # use a with statement when working with cursors
&amp;nbsp;&amp;nbsp;&amp;nbsp; # use the da module for faster access
&amp;nbsp;&amp;nbsp;&amp;nbsp; # use UpdateCursor to update values
&amp;nbsp;&amp;nbsp;&amp;nbsp; index = 0
&amp;nbsp;&amp;nbsp;&amp;nbsp; with arcpy.da.UpdateCursor(table, (fld_in, fld_out)) as curs:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in curs:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; name = row[0]
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if index == len(lst_values) - 1:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # last row, no 'next' value
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; next_name = None
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; next_name = lst_values[index + 1]

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # compare the values
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if name == next_name:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # they are the same, do something with this
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; out_value = 'some value you define'
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # they are different
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; out_value = 'some other value you define'

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # update the values
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; curs.updateRow((name, out_value, ))

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # increment index
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; index += 1


if __name__ == '__main__':
&amp;nbsp;&amp;nbsp;&amp;nbsp; main()&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 10:57:27 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/compare-value-from-n-and-n-1-row-in-searchcursor/m-p/224889#M17368</guid>
      <dc:creator>XanderBakker</dc:creator>
      <dc:date>2021-12-11T10:57:27Z</dc:date>
    </item>
    <item>
      <title>Re: Compare value from n and n+1 row in searchCursor</title>
      <link>https://community.esri.com/t5/python-questions/compare-value-from-n-and-n-1-row-in-searchcursor/m-p/224890#M17369</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;As pointed out, cursors can't predict the future, i.e., they have no idea what is in the next row until they visit the row, which then makes it the current row and not the next row.&amp;nbsp; Looking ahead with cursors involves iterating over the data set twice, either serially or in parallel.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="https://community.esri.com/migrated-users/3100" target="_blank"&gt;Xander Bakker&lt;/A&gt;'s code is serial, i.e., it loops through the data once, completely, to build a local lookup structure for the second loop.&amp;nbsp; The approach is straightforward and will work in a majority, possibly vast majority, of cases.&amp;nbsp; When working with very large data sets, especially with 32-bit Python, the size of the local copy of data may become an issue.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;An alternate approach that doesn't involve making local copies of data is to use two cursors in parallel where one increments ahead of the other as both go through the data set.&amp;nbsp; This eliminates the local copy of data, but it might create an I/O bottleneck with the back-end data provider depending on the data set and conditions.&amp;nbsp; Adapting from Xander's example:&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;# access data and define fields to use
mxd = arcpy.mapping.MapDocument("CURRENT")
table = arcpy.mapping.ListTableViews(mxd)[0]
fld_in = "name"
fld_out = "another field"&amp;nbsp; # I added a second field for output values

# use a with statement when working with cursors
# use the da module for faster access

# use UpdateCursor to update values
with arcpy.da.UpdateCursor(table, (fld_in, fld_out)) as u_cur:

&amp;nbsp;&amp;nbsp;&amp;nbsp; # use Search Cursor in parallel to get "next" value
&amp;nbsp;&amp;nbsp;&amp;nbsp; with arcpy.da.SearchCursor(table, fld_in) as s_cur:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; next(s_cur)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for s_fld_in, in s_cur:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; u_fld_in, u_fld_out = next(u_cur)

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # compare values and do one thing or another
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if s_fld_in == u_fld_in:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; u_fld_out = # some value
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; u_fld_out = # different value

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # update the values
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; u_cur.updateRow((u_fld_in, u_fld_out))&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;With a multiple, parallel cursor approach, it is a good idea to include an "ORDER BY" statement in the cursor definition to ensure the two cursors are iterating over the dataset in the same order.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Overall, I recommend structuring the data so you can look backwards instead of looking forwards.&amp;nbsp; Looking backwards involves a single cursor and single iteration, which is simpler to code and performs better.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 10:57:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/compare-value-from-n-and-n-1-row-in-searchcursor/m-p/224890#M17369</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2021-12-11T10:57:30Z</dc:date>
    </item>
    <item>
      <title>Re: Compare value from n and n+1 row in searchCursor</title>
      <link>https://community.esri.com/t5/python-questions/compare-value-from-n-and-n-1-row-in-searchcursor/m-p/224891#M17370</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;This code is backwards looking, not forwards looking.&amp;nbsp; Although I think structuring the data to support backwards looking is the best approach, if possible, the OP was asking about looking ahead in the data set.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 28 Jul 2016 15:28:25 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/compare-value-from-n-and-n-1-row-in-searchcursor/m-p/224891#M17370</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2016-07-28T15:28:25Z</dc:date>
    </item>
  </channel>
</rss>

