HOW TO
In some instances, users may want to select every other 'n' rows in a table to edit the selected features without having to select and populate the rows individually. This article demonstrates how to select every other 'n' rows using a Python script. In this example, the interval value is set to 3. A new layer with both selected and unselected features is created as the resulting output. In the attribute table for the new layer, three rows are selected, the next three rows are skipped and the same process is repeated until the end of all rows.
The result of this process is that half of the rows are selected from the original table, but these selections are specified as every other three rows. The following image shows the expected output:
import arcpy ##Replace data path## fc = r'C:\EsriTraining\SpatialProb\CraterLake.gdb\Roads' ##Set desired interval. interval = 1 selects every other row, interval = 2 selects 2, skips 2,## ##interval = 3 selects 3, skips 3 and so on.## interval = 3 selected_oid = [] ready_to_add = False interval = abs(interval) ##Replace layer_name if desired## fc = arcpy.MakeFeatureLayer_management(fc, "layer_name") with arcpy.da.SearchCursor(fc, 'OID@') as cur: for i, row in enumerate(cur): if i % interval ==0: ready_to_add = not ready_to_add if ready_to_add: selected_oid.append(unicode(row[0])) where = 'OBJECTID in ({})'.format(', '.join(selected_oid)) selected_fc = arcpy.SelectLayerByAttribute_management(fc, "NEW_SELECTION", where)
Get help from ArcGIS experts
Download the Esri Support App