AnsweredAssumed Answered

Using Cursors and a dictionary to sum numerical values and populate a new field

Question asked by tkokenge on May 14, 2012
Latest reply on May 14, 2012 by tkokenge
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.

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.

The AC_GIS field is type double and LOT_AC_SUM_GIS is type float. 

Any help or ideas? Thanks.

# create dictionary of listing number count and status change date Acres_dict = {}  acursor = arcpy.SearchCursor(Merge_counties_output) for arow in acursor:     Acres_dict[arow.Listing_Number] = [0,0]     if arow.Listing_Number not in Acres_dict:         Acres_dict[arow.Listing_Number][0] = arow.AC_GIS         Acres_dict[arow.Listing_Number][1] = 1     else:         Acres_dict[arow.Listing_Number][0] = Acres_dict[arow.Listing_Number][0] + arow.AC_GIS         Acres_dict[arow.Listing_Number][1] = Acres_dict[arow.Listing_Number][1] + 1          del arow del acursor print "create dictionary ... finished!"  # Calculate LOT_AC_SUM_GIS scursor = arcpy.UpdateCursor(Merge_counties_output) for srow in scursor:     if Acres_dict[srow.Listing_Number][1] > 1:         srow.setValue("LOT_AC_SUM_GIS", Acres_dict[srow.Listing_Number][0])         scursor.updateRow(srow)     else:         srow.setValue("LOT_AC_SUM_GIS", srow.AC_GIS)         scursor.updateRow(srow)                        del srow del scursor del Acres_dict print "Sum Acres field ... calculated!"