<?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: Using Cursors and a dictionary to sum numerical values and populate a new field in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/using-cursors-and-a-dictionary-to-sum-numerical/m-p/38189#M2981</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Your code is initializing the dictionary element to [0,0] for every iteration.&amp;nbsp; This is overwriting the previous results.&amp;nbsp; Try the following:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;# create dictionary of listing number count and status change date Acres_dict = {}&amp;nbsp; acursor = arcpy.SearchCursor(Merge_counties_output) for arow in acursor: &amp;nbsp;&amp;nbsp;&amp;nbsp; if arow.Listing_Number not in Acres_dict: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Acres_dict[arow.Listing_Number] = [0,0] &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Acres_dict[arow.Listing_Number][0] = arow.AC_GIS &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Acres_dict[arow.Listing_Number][1] = 1 &amp;nbsp;&amp;nbsp;&amp;nbsp; else: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Acres_dict[arow.Listing_Number][0] = Acres_dict[arow.Listing_Number][0] + arow.AC_GIS &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Acres_dict[arow.Listing_Number][1] = Acres_dict[arow.Listing_Number][1] + 1 &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; del arow del acursor print "create dictionary ... finished!"&amp;nbsp; # Calculate LOT_AC_SUM_GIS scursor = arcpy.UpdateCursor(Merge_counties_output) for srow in scursor: &amp;nbsp;&amp;nbsp;&amp;nbsp; if Acres_dict[srow.Listing_Number][1] &amp;gt; 1: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; srow.setValue("LOT_AC_SUM_GIS", Acres_dict[srow.Listing_Number][0]) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; scursor.updateRow(srow) &amp;nbsp;&amp;nbsp;&amp;nbsp; else: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; srow.setValue("LOT_AC_SUM_GIS", srow.AC_GIS) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; scursor.updateRow(srow) &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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; del srow del scursor del Acres_dict print "Sum Acres field ... calculated!"&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Mon, 14 May 2012 19:37:28 GMT</pubDate>
    <dc:creator>Anonymous User</dc:creator>
    <dc:date>2012-05-14T19:37:28Z</dc:date>
    <item>
      <title>Using Cursors and a dictionary to sum numerical values and populate a new field</title>
      <link>https://community.esri.com/t5/python-questions/using-cursors-and-a-dictionary-to-sum-numerical/m-p/38188#M2980</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I am trying to populate a field with the sum of the acres of another field. I only need to sum the acres when the listing number appears more than once, otherwise I simply want to transfer the value directly from the AC_GIS field. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;When I run the code, the result shows that all the values are being copied from the AC_GIS field and when it should be summing (when there is multiple records with the same listing number) it is not doing the sum. Therefore, I believe there must be something wrong with the SearchCursor or the if statement in the UpdateCursor. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The AC_GIS field is type double and LOT_AC_SUM_GIS is type float.&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Any help or ideas? Thanks.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;# create dictionary of listing number count and status change date Acres_dict = {}&amp;nbsp; acursor = arcpy.SearchCursor(Merge_counties_output) for arow in acursor: &amp;nbsp;&amp;nbsp;&amp;nbsp; Acres_dict[arow.Listing_Number] = [0,0] &amp;nbsp;&amp;nbsp;&amp;nbsp; if arow.Listing_Number not in Acres_dict: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Acres_dict[arow.Listing_Number][0] = arow.AC_GIS &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Acres_dict[arow.Listing_Number][1] = 1 &amp;nbsp;&amp;nbsp;&amp;nbsp; else: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Acres_dict[arow.Listing_Number][0] = Acres_dict[arow.Listing_Number][0] + arow.AC_GIS &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Acres_dict[arow.Listing_Number][1] = Acres_dict[arow.Listing_Number][1] + 1 &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; del arow del acursor print "create dictionary ... finished!"&amp;nbsp; # Calculate LOT_AC_SUM_GIS scursor = arcpy.UpdateCursor(Merge_counties_output) for srow in scursor: &amp;nbsp;&amp;nbsp;&amp;nbsp; if Acres_dict[srow.Listing_Number][1] &amp;gt; 1: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; srow.setValue("LOT_AC_SUM_GIS", Acres_dict[srow.Listing_Number][0]) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; scursor.updateRow(srow) &amp;nbsp;&amp;nbsp;&amp;nbsp; else: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; srow.setValue("LOT_AC_SUM_GIS", srow.AC_GIS) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; scursor.updateRow(srow) &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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; del srow del scursor del Acres_dict print "Sum Acres field ... calculated!"&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 14 May 2012 18:07:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/using-cursors-and-a-dictionary-to-sum-numerical/m-p/38188#M2980</guid>
      <dc:creator>TrevorKokenge</dc:creator>
      <dc:date>2012-05-14T18:07:30Z</dc:date>
    </item>
    <item>
      <title>Re: Using Cursors and a dictionary to sum numerical values and populate a new field</title>
      <link>https://community.esri.com/t5/python-questions/using-cursors-and-a-dictionary-to-sum-numerical/m-p/38189#M2981</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Your code is initializing the dictionary element to [0,0] for every iteration.&amp;nbsp; This is overwriting the previous results.&amp;nbsp; Try the following:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;# create dictionary of listing number count and status change date Acres_dict = {}&amp;nbsp; acursor = arcpy.SearchCursor(Merge_counties_output) for arow in acursor: &amp;nbsp;&amp;nbsp;&amp;nbsp; if arow.Listing_Number not in Acres_dict: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Acres_dict[arow.Listing_Number] = [0,0] &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Acres_dict[arow.Listing_Number][0] = arow.AC_GIS &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Acres_dict[arow.Listing_Number][1] = 1 &amp;nbsp;&amp;nbsp;&amp;nbsp; else: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Acres_dict[arow.Listing_Number][0] = Acres_dict[arow.Listing_Number][0] + arow.AC_GIS &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Acres_dict[arow.Listing_Number][1] = Acres_dict[arow.Listing_Number][1] + 1 &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; del arow del acursor print "create dictionary ... finished!"&amp;nbsp; # Calculate LOT_AC_SUM_GIS scursor = arcpy.UpdateCursor(Merge_counties_output) for srow in scursor: &amp;nbsp;&amp;nbsp;&amp;nbsp; if Acres_dict[srow.Listing_Number][1] &amp;gt; 1: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; srow.setValue("LOT_AC_SUM_GIS", Acres_dict[srow.Listing_Number][0]) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; scursor.updateRow(srow) &amp;nbsp;&amp;nbsp;&amp;nbsp; else: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; srow.setValue("LOT_AC_SUM_GIS", srow.AC_GIS) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; scursor.updateRow(srow) &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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; del srow del scursor del Acres_dict print "Sum Acres field ... calculated!"&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 14 May 2012 19:37:28 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/using-cursors-and-a-dictionary-to-sum-numerical/m-p/38189#M2981</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2012-05-14T19:37:28Z</dc:date>
    </item>
    <item>
      <title>Re: Using Cursors and a dictionary to sum numerical values and populate a new field</title>
      <link>https://community.esri.com/t5/python-questions/using-cursors-and-a-dictionary-to-sum-numerical/m-p/38190#M2982</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;That did the trick. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks josborne. Now how do I award you points, or does ESRI take care of that?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 14 May 2012 20:10:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/using-cursors-and-a-dictionary-to-sum-numerical/m-p/38190#M2982</guid>
      <dc:creator>TrevorKokenge</dc:creator>
      <dc:date>2012-05-14T20:10:17Z</dc:date>
    </item>
  </channel>
</rss>

