AnsweredAssumed Answered

Loop through records based on unique value and stopping once max value is met

Question asked by LuisRaulResh on Aug 14, 2019
Latest reply on Aug 15, 2019 by Dan_Patterson

I am trying to loop through records of a feature class with same site number ID's, perform a set of arcpy functions, and stop loop and continue on with later code once max ID is met. 

 

I used a cursor to find and set a max value variable of largest number from feature class Site ID field and then a for loop to iterate over the feature class to select records with the same site ID, perform acouple of functions and then stop once max value is met. Problem is the code keeps looping/creating FC's past the max value (which is 37 in the attribute table). Any advice?

 

Code Below:

 

arcpy.CreateFeatureDataset_management(Output_Location,"MapGrids",spatref)
max_value = -999999
with arcpy.da.SearchCursor("Merged_Assets", "Site_Num") as cursor:
    for row in cursor:
        max_value = max(int(row[0]), max_value)

count = 1
if count <= (max_value):  
    for row in cursor:
      arcpy.SelectLayerByAttribute_management("Merged_Assets","NEW_SELECTION",'"Site_Num" = \'{0}\''.format(count))
      arcpy.GridIndexFeatures_cartography(Output_Location + "\MapGrids" + "\Site" + str(count),"Merged_Assets","INTERSECTFEATURE","USEPAGEUNIT")
        arcpy.AddField_management(Output_Location + "\MapGrids" + "\Site" + str(count),"Site_Num","TEXT")
        countexp = count
        arcpy.CalculateField_management(Output_Location + "\MapGrids" + "\Site" + str(count),"Site_Num","'" + str(countexp) + "'","PYTHON_9.3")
        count = count + 1

Outcomes