AnsweredAssumed Answered

Counting items that go through your loop

Question asked by matthewrusso on Feb 26, 2015
Latest reply on Feb 27, 2015 by blake.terhune

As I dive further into Python, I find myself making a "GIS Module" that calls functions that are useful to my work.

 

I created a quick find and replace function but i want to print off the total number items replaced.

 

I also have a show unique value function that lists all the unique items in a field and their values.  I want to include just a simple line like:

 

547 items replaced

 

here are my 2 functions can anyone explain how to include this:

 

 

 

def field_counter(table,fieldname):
     values = [r[0] for r in arcpy.da.SearchCursor(table, [fieldname])]
     unique_values = set(values)
     count_dict = {}
     for value in unique_values:
          count_dict[value] = values.count(value)
          print("{0} Unique Value: {1} has {2} attribute(s)".format(fieldname, value, count_dict[value]))

def find_and_replace(table, field, find, replace):
     with arcpy.da.UpdateCursor(table, [field]) as cursor:
          for row in cursor:
               if row[0] == find:
               row[0] = replace
               cursor.updateRow(row)
               print(row[0] + ' was replaced by' + replace)
          elif row[0] != find:
               print(row[0] + ' was not replaced')

Outcomes