0

I am trying to create a Cursor that goes through a selection process and stops looping when only one feature is selected. Basically, as the Cursor loops it subsets a selection, and instead of going through every row of the table I'd like to save time and stop the Cursor when one item is selected. So it would look something like this:

for row in rows:
    ... (selecting process) ...
    if [number of features selected] = 1:
        [stop cursor]
    else:
        rows.updateRow(row)
del row, rows

Any advice? I can't seem to even find any way to stop a cursor other than deleting it but I don't think I could do that type of command from within the cursor itself.

3
  • Cursors opened on layers with selections already only process the selected rows. Is this not what you are seeing? Post the complete code if so.
    – blah238
    Feb 12, 2013 at 19:25
  • Why is the ... (selecting process) ... inside of the loop?
    – blah238
    Feb 12, 2013 at 19:25
  • See also: Accessing data using cursors
    – blah238
    Feb 12, 2013 at 19:27

1 Answer 1

4

I usually do this using arcpy.GetCount_management()

If you set it up like this:

if int(arcpy.GetCount_management(lyr_Name).getOutput(0)) == 1:
    break
else:
    [whatever else you were going to do]

It should kick your code out of the loop when the number of selected features in lyr_Name is one.

Here's the help page from ESRI for the function.

1
  • Glad it was helpful!
    – Kevin
    Feb 18, 2013 at 20:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.