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

624
2
Jump to solution
05-14-2012 11:07 AM
TrevorKokenge
New Contributor
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!"
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
by Anonymous User
Not applicable
Your code is initializing the dictionary element to [0,0] for every iteration.  This is overwriting the previous results.  Try the following:


# create dictionary of listing number count and status change date Acres_dict = {}  acursor = arcpy.SearchCursor(Merge_counties_output) for arow in acursor:     if arow.Listing_Number not in Acres_dict:         Acres_dict[arow.Listing_Number] = [0,0]         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!"

View solution in original post

0 Kudos
2 Replies
by Anonymous User
Not applicable
Your code is initializing the dictionary element to [0,0] for every iteration.  This is overwriting the previous results.  Try the following:


# create dictionary of listing number count and status change date Acres_dict = {}  acursor = arcpy.SearchCursor(Merge_counties_output) for arow in acursor:     if arow.Listing_Number not in Acres_dict:         Acres_dict[arow.Listing_Number] = [0,0]         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!"
0 Kudos
TrevorKokenge
New Contributor
That did the trick.

Thanks josborne. Now how do I award you points, or does ESRI take care of that?
0 Kudos