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.

asked Feb 12, 2013 at 19:19
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. Commented Feb 12, 2013 at 19:25
  • Why is the ... (selecting process) ... inside of the loop? Commented Feb 12, 2013 at 19:25
  • See also: Accessing data using cursors Commented 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.

answered Feb 12, 2013 at 19:25
1
  • Glad it was helpful! Commented Feb 18, 2013 at 20:04

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.