Solved! Go to Solution.
def findFields(inputFC): fields = arcpy.ListFields(inputFC) # list of desired fields outFields = [] for field in fields: # if NLCD in the field name, add to the list of fields if 'NLCD' in field.name: outFields.append(field.name) return outFields
## Get input feature class as inputFC outputFieldName = 'Count' # add output field arcpy.AddField_management(inputFC, outputFieldName, 'LONG') # list of fields in input FC inFields = arcpy.ListFields(inputFC) # list of desired fields - output field is index 0 sumFields = [] for field in inFields: # if NLCD in the field name, add to the list of fields if 'NLCD' in field.name: sumFields.append(field.name) # use DA update cursor to add sum of values to Count field # will only work with Arc 10.1, there is a slightly different tool for 10.1 with arcpy.da.UpdateCursor(inputFC, [outputFieldName] + sumFields) as cursor: for row in cursor: # sum the NLCD fields, no matter how many of them there are (+1 skips the output field) row[0] = sum([row[i+1] for i in range(len(sumFields))]) cursor.updateRow(row) ## pass inputFC back as an output
def findFields(inputFC): fields = arcpy.ListFields(inputFC) # list of desired fields outFields = [] for field in fields: # if NLCD in the field name, add to the list of fields if 'NLCD' in field.name: outFields.append(field.name) return outFields
## Get input feature class as inputFC outputFieldName = 'Count' # add output field arcpy.AddField_management(inputFC, outputFieldName, 'LONG') # list of fields in input FC inFields = arcpy.ListFields(inputFC) # list of desired fields - output field is index 0 sumFields = [] for field in inFields: # if NLCD in the field name, add to the list of fields if 'NLCD' in field.name: sumFields.append(field.name) # use DA update cursor to add sum of values to Count field # will only work with Arc 10.1, there is a slightly different tool for 10.1 with arcpy.da.UpdateCursor(inputFC, [outputFieldName] + sumFields) as cursor: for row in cursor: # sum the NLCD fields, no matter how many of them there are (+1 skips the output field) row[0] = sum([row[i+1] for i in range(len(sumFields))]) cursor.updateRow(row) ## pass inputFC back as an output