loop terminates

962
13
Jump to solution
09-21-2017 03:28 AM
TomaszChojnacki
New Contributor

Hello,

I am trying to perform some editing on features that have the same attribute value  as features of a different feature class. So I tried this:

 

field0 = 'SKMS_tor'
field1 = 'SKMS'
cursor0 = arcpy.da.SearchCursor(fc0, field0)
cursor1 = arcpy.da.SearchCursor(fc1, field1)

for row0 in cursor0:
    for row1 in cursor1:

        if row0 == row1:

            do do editing

        else:

            skip editing

 

I works, but only for the first round. Then it stops and does not proceed to the second feature in the feature class. Not sure what is wrong with this script.

 

I tested the same syntax on a different example:

 

q = ['a1', 'a2', 'a3']
w = ['a3', 'a1', 'b2']
for x in q:
    for y in w:
        if x == y:
            print x, " YES ", y
        else:
            print x, " NO ", y

 

and it works fine.

 

To my novice eye there is no difference in both scripts except the use arcpy.da.SearchCursor(). Can this be a problem? if not, what else?

 

Thanks for help.

0 Kudos
1 Solution

Accepted Solutions
JoshuaBixby
MVP Esteemed Contributor

Have you tried resetting the cursor yet?

field0 = 'SKMS_tor'
field1 = 'SKMS'
cursor0 = arcpy.da.SearchCursor(fc0, field0)
cursor1 = arcpy.da.SearchCursor(fc1, field1)

for row0 in cursor0:
    for row1 in cursor1:

        if row0 == row1:

            do do editing

        else:

            skip editing

     cursor1.reset()

View solution in original post

13 Replies
JoeBorgione
MVP Esteemed Contributor

Don't have an answer for you Tomasz, but I do have a question; which of the cursors stop?  Cursor0 or Cursor1?  Just yesterday I was faced with a similar problem where I want to edit one table based on it's interaction with another feature class.

Let's call in the guys that have helped me in the not so distant past with cursors:

curtvprice‌, dkwiens‌, Dan_Patterson‌, jskinner-esristaff

and see what they might say....

That should just about do it....
0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

sounds like nested cursor issues.

Don't you cursor types use 'while' loops? or do you have to call a next()?  I am sure the others have examples, but here is the docs  If it data were not in the millions of records, I would just read both into arrays/lists, do the comparisons and keep an index of what to change for processing after, rather than on the go

JoeBorgione
MVP Esteemed Contributor

Darren Wiens‌ provided this link yesterday:  /blogs/richard_fairhurst/2014/11/08/turbo-charging-data-manipulation-with-python-cursors-and-diction... 

I'm about to get into it....

That should just about do it....
JoshuaBixby
MVP Esteemed Contributor

Overall, the approach is not very efficient, but I only have time now to speak to the specific error you are seeing.  You need to reset cursor1 before going to the next row of cursor0.  I don't know what is entailed in "do do editing," but you won't be able to do any updates to either data set using search cursors.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Have you tried resetting the cursor yet?

field0 = 'SKMS_tor'
field1 = 'SKMS'
cursor0 = arcpy.da.SearchCursor(fc0, field0)
cursor1 = arcpy.da.SearchCursor(fc1, field1)

for row0 in cursor0:
    for row1 in cursor1:

        if row0 == row1:

            do do editing

        else:

            skip editing

     cursor1.reset()
TomaszChojnacki
New Contributor

This is what I needed. Thanks Joshua!

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Please mark a response as the answer to close the question out.  Also, if you haven't already, give a helpful to other responses that were, well, helpful.

0 Kudos
RandyBurton
MVP Regular Contributor

I would also suggest Turbo Charging Data Manipulation with Python Cursors and Dictionaries (see Example 1).

Using a search cursor, you would load a dictionary with values contained in the SKMS_for field along with the objectID or globalID for that dataset.

Then using an update cursor, if the field value is in the dictionary, you can do your editing or mark the row for later action.

0 Kudos
TomaszChojnacki
New Contributor

Thank you Randy, 

Usefull stuff and I need to get into this. Lots of learning for me as a complete novice!

0 Kudos