Solved! Go to Solution.
import arcpy from arcpy import env env.overwriteOutput = True env.workspace = r"C:\Users\test\Desktop\TestPythonScripts" table = "data.dbf" list = [] # iterate through table and append ID to list with arcpy.da.SearchCursor(table, ["FEAT_SEQ"]) as cursor: for row in cursor: list.append(row[0]) del row, cursor #remove duplicate IDs list = dict.fromkeys(list) list = list.keys() list2 = [] for FEAT_SEQ in list: with arcpy.da.SearchCursor(table, ["FEAT_SEQ", "YEAR_OF_BI", "DIFFERENCE"],"FEAT_SEQ = " +str(FEAT_SEQ)) as cursor: for row in cursor: #append YEAR_OF_BI to new list list2.append(row[1]) #sort list list2.sort() try: #calculate the difference DIFFERENCE = list2[1] - list2[0] print list2[1] print list2[0] print DIFFERENCE except: DIFFERENCE = '' del row, cursor try: #update table with difference value with arcpy.da.UpdateCursor(table, ["FEAT_SEQ", "YEAR_OF_BI", "DIFFERENCE"], "FEAT_SEQ = " + str(FEAT_SEQ) + "AND YEAR_OF_BI = " + str(list2[0]) + " OR FEAT_SEQ = " + str(FEAT_SEQ) + "AND YEAR_OF_BI = " + str(list2[1])) as cursor: for row in cursor: row[2] = DIFFERENCE cursor.updateRow(row) print row del row, cursor except: pass print "Finished Script"
table = "data.dbf" list = [] # iterate through table and append ID to list with arcpy.da.SearchCursor(table, ["FEAT_SEQ"]) as cursor: for row in cursor: list.append(row[0]) del row, cursor #remove duplicate IDs list = dict.fromkeys(list) list = list.keys() for FEAT_SEQ in list: list2 = [] with arcpy.da.SearchCursor(table, ["FEAT_SEQ", "YEAR_OF_BI", "DIFFERENCE"],"FEAT_SEQ = " +str(FEAT_SEQ)) as cursor: for row in cursor: #append YEAR_OF_BI to new list list2.append(row[1]) #sort list list2.sort() try: #calculate the difference DIFFERENCE = int(list2[-1]) - int(list2[0]) except: DIFFERENCE = '' del row, cursor try: #update table with difference value with arcpy.da.UpdateCursor(table, ["FEAT_SEQ", "YEAR_OF_BI", "DIFFERENCE"], "FEAT_SEQ = " + str(FEAT_SEQ) + "AND YEAR_OF_BI = " + str(list2[0]) + " OR FEAT_SEQ = " + str(FEAT_SEQ) + "AND YEAR_OF_BI = " + str(list2[1])) as cursor: for row in cursor: row[2] = DIFFERENCE cursor.updateRow(row) del row, cursor except: pass print "Finished Script"