Using cursor.next() is resetting “row in cursor” causing it to skip every other row The Next CEO of Stack OverflowHow would I group values in a field based on range of number using python, and then update the attribute table?How to check for empty values in fields of a featureclass?Calculate and Restart Sequential ID for Unique Values in a Fieldarcpy.UpdateCursor updates every rowCursor update row operatorWriting Data Driven Pages' scale into driving layer's attribute table with arcpyPopulate field with information from a different field based on matching recordStratified random point sampling in PythonComparing value with value from the next rowDeleting row using ArcPy cursor?

How long to clear the 'suck zone' of a turbofan after start is initiated?

Visit to the USA with ESTA approved before trip to Iran

Customer Requests (Sometimes) Drive Me Bonkers!

When airplanes disconnect from a tanker during air to air refueling, why do they bank so sharply to the right?

Whats the best way to handle refactoring a big file?

How easy is it to start Magic from scratch?

How to start emacs in "nothing" mode (`fundamental-mode`)

How to get regions to plot as graphics

Would this house-rule that treats advantage as a +1 to the roll instead (and disadvantage as -1) and allows them to stack be balanced?

Can the Reverse Gravity spell affect the Meteor Swarm spell?

Apart from "berlinern", do any other German dialects have a corresponding verb?

Natural language into sentence logic

Implement the Thanos sorting algorithm

How do I go from 300 unfinished/half written blog posts, to published posts?

Why here is plural "We went to the movies last night."

Go Pregnant or Go Home

Is it safe to use c_str() on a temporary string?

Opposite of a diet

Robert Sheckley short story about vacation spots being overwhelmed

Shade part of a Venn diagram

The King's new dress

Why didn't Khan get resurrected in the Genesis Explosion?

How to use tikz in fbox?

Why does C# sound extremely flat when saxophone is tuned to G?



Using cursor.next() is resetting “row in cursor” causing it to skip every other row



The Next CEO of Stack OverflowHow would I group values in a field based on range of number using python, and then update the attribute table?How to check for empty values in fields of a featureclass?Calculate and Restart Sequential ID for Unique Values in a Fieldarcpy.UpdateCursor updates every rowCursor update row operatorWriting Data Driven Pages' scale into driving layer's attribute table with arcpyPopulate field with information from a different field based on matching recordStratified random point sampling in PythonComparing value with value from the next rowDeleting row using ArcPy cursor?










0















I'm trying to update a column based on the value in the next row, but cursor.next() is giving unexpected behavior. When I identify the next_row using cursor.next(), it is resetting my row in cursor! The result is my loop goes over every other row, not every row. What am I missing? (Code below is just to show cursor behavior; code for row update for column has been stripped.)



Code



import arcpy
temp1 = r'mypathmypointfile.shp'
with arcpy.da.UpdateCursor(temp1, ['OID@']) as cursor:
for row in cursor:
try:
row_next = cursor.next()
print 'current_oid=', row[0], 'next_oid=', row_next[0]
except StopIteration:
print 'no rows'
except:
print 'something else went wrong'


Actual Prints



current_oid= 0 next_oid= 1
current_oid= 2 next_oid= 3
current_oid= 4 next_oid= 5
current_oid= 6 next_oid= 7
current_oid= 8 next_oid= 9


Expected Prints



current_oid= 0 next_oid= 1
current_oid= 1 next_oid= 2
current_oid= 2 next_oid= 3
current_oid= 3 next_oid= 4
current_oid= 4 next_oid= 5


UPDATE



Purpose of this is to only keep rows that are the first or last point in the group.



Example attributes:



oid group id
0 1 1
1 1 2
2 1 3
3 2 1
4 2 2
5 3 1
6 3 2
7 3 3
8 3 4
9 3 5


Desired attributes:



group id
1 1
1 3
2 1
2 2
3 1
3 5


Original code, that didn't work because cursor.next() skips rows:



with arcpy.da.UpdateCursor(temp1, ['OID@', 'group', 'id']) as cursor:
for row in cursor:
group_current = row[1]
id_current = row[2]
if id_current == 1:
check = "keep"
else:
try:
row_next = cursor.next()
group_next = row_next[1]
id_next = row_next[2]
if id_next == 1:
check = "keep"
else:
check = "remove"
except StopIteration:
check = "keep"
except:
print 'something else went wrong'
if check != "keep":
cursor.deleteRow()









share|improve this question



















  • 2





    next isn't needed, because you're in a for loop. You can't read ahead, but you can preserve the current in previous then act on that later.

    – Vince
    yesterday












  • @Vince As stated in question, purpose is to perform UpdateCursor on a specific column based on values in another column. The printing of the oid values was for illustration of skipping behavior. I will be calculating the value in "mycol" in row4 based on the value in "myotherCol" in row5.

    – AlexS1
    yesterday






  • 1





    No, you can't read ahead without losing the current row. You need to use cursor as it actually operates, not how you wish it did. What you can do is save your values in a dictionary with a SearchCursor scan with an OID key, the run an update pass later. I've cached tens of millions of rows with a 32-bit Python and hundreds of millions with 64-bit Python, so 15k rows is a drop in the bucket.

    – Vince
    yesterday















0















I'm trying to update a column based on the value in the next row, but cursor.next() is giving unexpected behavior. When I identify the next_row using cursor.next(), it is resetting my row in cursor! The result is my loop goes over every other row, not every row. What am I missing? (Code below is just to show cursor behavior; code for row update for column has been stripped.)



Code



import arcpy
temp1 = r'mypathmypointfile.shp'
with arcpy.da.UpdateCursor(temp1, ['OID@']) as cursor:
for row in cursor:
try:
row_next = cursor.next()
print 'current_oid=', row[0], 'next_oid=', row_next[0]
except StopIteration:
print 'no rows'
except:
print 'something else went wrong'


Actual Prints



current_oid= 0 next_oid= 1
current_oid= 2 next_oid= 3
current_oid= 4 next_oid= 5
current_oid= 6 next_oid= 7
current_oid= 8 next_oid= 9


Expected Prints



current_oid= 0 next_oid= 1
current_oid= 1 next_oid= 2
current_oid= 2 next_oid= 3
current_oid= 3 next_oid= 4
current_oid= 4 next_oid= 5


UPDATE



Purpose of this is to only keep rows that are the first or last point in the group.



Example attributes:



oid group id
0 1 1
1 1 2
2 1 3
3 2 1
4 2 2
5 3 1
6 3 2
7 3 3
8 3 4
9 3 5


Desired attributes:



group id
1 1
1 3
2 1
2 2
3 1
3 5


Original code, that didn't work because cursor.next() skips rows:



with arcpy.da.UpdateCursor(temp1, ['OID@', 'group', 'id']) as cursor:
for row in cursor:
group_current = row[1]
id_current = row[2]
if id_current == 1:
check = "keep"
else:
try:
row_next = cursor.next()
group_next = row_next[1]
id_next = row_next[2]
if id_next == 1:
check = "keep"
else:
check = "remove"
except StopIteration:
check = "keep"
except:
print 'something else went wrong'
if check != "keep":
cursor.deleteRow()









share|improve this question



















  • 2





    next isn't needed, because you're in a for loop. You can't read ahead, but you can preserve the current in previous then act on that later.

    – Vince
    yesterday












  • @Vince As stated in question, purpose is to perform UpdateCursor on a specific column based on values in another column. The printing of the oid values was for illustration of skipping behavior. I will be calculating the value in "mycol" in row4 based on the value in "myotherCol" in row5.

    – AlexS1
    yesterday






  • 1





    No, you can't read ahead without losing the current row. You need to use cursor as it actually operates, not how you wish it did. What you can do is save your values in a dictionary with a SearchCursor scan with an OID key, the run an update pass later. I've cached tens of millions of rows with a 32-bit Python and hundreds of millions with 64-bit Python, so 15k rows is a drop in the bucket.

    – Vince
    yesterday













0












0








0








I'm trying to update a column based on the value in the next row, but cursor.next() is giving unexpected behavior. When I identify the next_row using cursor.next(), it is resetting my row in cursor! The result is my loop goes over every other row, not every row. What am I missing? (Code below is just to show cursor behavior; code for row update for column has been stripped.)



Code



import arcpy
temp1 = r'mypathmypointfile.shp'
with arcpy.da.UpdateCursor(temp1, ['OID@']) as cursor:
for row in cursor:
try:
row_next = cursor.next()
print 'current_oid=', row[0], 'next_oid=', row_next[0]
except StopIteration:
print 'no rows'
except:
print 'something else went wrong'


Actual Prints



current_oid= 0 next_oid= 1
current_oid= 2 next_oid= 3
current_oid= 4 next_oid= 5
current_oid= 6 next_oid= 7
current_oid= 8 next_oid= 9


Expected Prints



current_oid= 0 next_oid= 1
current_oid= 1 next_oid= 2
current_oid= 2 next_oid= 3
current_oid= 3 next_oid= 4
current_oid= 4 next_oid= 5


UPDATE



Purpose of this is to only keep rows that are the first or last point in the group.



Example attributes:



oid group id
0 1 1
1 1 2
2 1 3
3 2 1
4 2 2
5 3 1
6 3 2
7 3 3
8 3 4
9 3 5


Desired attributes:



group id
1 1
1 3
2 1
2 2
3 1
3 5


Original code, that didn't work because cursor.next() skips rows:



with arcpy.da.UpdateCursor(temp1, ['OID@', 'group', 'id']) as cursor:
for row in cursor:
group_current = row[1]
id_current = row[2]
if id_current == 1:
check = "keep"
else:
try:
row_next = cursor.next()
group_next = row_next[1]
id_next = row_next[2]
if id_next == 1:
check = "keep"
else:
check = "remove"
except StopIteration:
check = "keep"
except:
print 'something else went wrong'
if check != "keep":
cursor.deleteRow()









share|improve this question
















I'm trying to update a column based on the value in the next row, but cursor.next() is giving unexpected behavior. When I identify the next_row using cursor.next(), it is resetting my row in cursor! The result is my loop goes over every other row, not every row. What am I missing? (Code below is just to show cursor behavior; code for row update for column has been stripped.)



Code



import arcpy
temp1 = r'mypathmypointfile.shp'
with arcpy.da.UpdateCursor(temp1, ['OID@']) as cursor:
for row in cursor:
try:
row_next = cursor.next()
print 'current_oid=', row[0], 'next_oid=', row_next[0]
except StopIteration:
print 'no rows'
except:
print 'something else went wrong'


Actual Prints



current_oid= 0 next_oid= 1
current_oid= 2 next_oid= 3
current_oid= 4 next_oid= 5
current_oid= 6 next_oid= 7
current_oid= 8 next_oid= 9


Expected Prints



current_oid= 0 next_oid= 1
current_oid= 1 next_oid= 2
current_oid= 2 next_oid= 3
current_oid= 3 next_oid= 4
current_oid= 4 next_oid= 5


UPDATE



Purpose of this is to only keep rows that are the first or last point in the group.



Example attributes:



oid group id
0 1 1
1 1 2
2 1 3
3 2 1
4 2 2
5 3 1
6 3 2
7 3 3
8 3 4
9 3 5


Desired attributes:



group id
1 1
1 3
2 1
2 2
3 1
3 5


Original code, that didn't work because cursor.next() skips rows:



with arcpy.da.UpdateCursor(temp1, ['OID@', 'group', 'id']) as cursor:
for row in cursor:
group_current = row[1]
id_current = row[2]
if id_current == 1:
check = "keep"
else:
try:
row_next = cursor.next()
group_next = row_next[1]
id_next = row_next[2]
if id_next == 1:
check = "keep"
else:
check = "remove"
except StopIteration:
check = "keep"
except:
print 'something else went wrong'
if check != "keep":
cursor.deleteRow()






arcpy cursor arcgis-10.6






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited yesterday







AlexS1

















asked yesterday









AlexS1AlexS1

16710




16710







  • 2





    next isn't needed, because you're in a for loop. You can't read ahead, but you can preserve the current in previous then act on that later.

    – Vince
    yesterday












  • @Vince As stated in question, purpose is to perform UpdateCursor on a specific column based on values in another column. The printing of the oid values was for illustration of skipping behavior. I will be calculating the value in "mycol" in row4 based on the value in "myotherCol" in row5.

    – AlexS1
    yesterday






  • 1





    No, you can't read ahead without losing the current row. You need to use cursor as it actually operates, not how you wish it did. What you can do is save your values in a dictionary with a SearchCursor scan with an OID key, the run an update pass later. I've cached tens of millions of rows with a 32-bit Python and hundreds of millions with 64-bit Python, so 15k rows is a drop in the bucket.

    – Vince
    yesterday












  • 2





    next isn't needed, because you're in a for loop. You can't read ahead, but you can preserve the current in previous then act on that later.

    – Vince
    yesterday












  • @Vince As stated in question, purpose is to perform UpdateCursor on a specific column based on values in another column. The printing of the oid values was for illustration of skipping behavior. I will be calculating the value in "mycol" in row4 based on the value in "myotherCol" in row5.

    – AlexS1
    yesterday






  • 1





    No, you can't read ahead without losing the current row. You need to use cursor as it actually operates, not how you wish it did. What you can do is save your values in a dictionary with a SearchCursor scan with an OID key, the run an update pass later. I've cached tens of millions of rows with a 32-bit Python and hundreds of millions with 64-bit Python, so 15k rows is a drop in the bucket.

    – Vince
    yesterday







2




2





next isn't needed, because you're in a for loop. You can't read ahead, but you can preserve the current in previous then act on that later.

– Vince
yesterday






next isn't needed, because you're in a for loop. You can't read ahead, but you can preserve the current in previous then act on that later.

– Vince
yesterday














@Vince As stated in question, purpose is to perform UpdateCursor on a specific column based on values in another column. The printing of the oid values was for illustration of skipping behavior. I will be calculating the value in "mycol" in row4 based on the value in "myotherCol" in row5.

– AlexS1
yesterday





@Vince As stated in question, purpose is to perform UpdateCursor on a specific column based on values in another column. The printing of the oid values was for illustration of skipping behavior. I will be calculating the value in "mycol" in row4 based on the value in "myotherCol" in row5.

– AlexS1
yesterday




1




1





No, you can't read ahead without losing the current row. You need to use cursor as it actually operates, not how you wish it did. What you can do is save your values in a dictionary with a SearchCursor scan with an OID key, the run an update pass later. I've cached tens of millions of rows with a 32-bit Python and hundreds of millions with 64-bit Python, so 15k rows is a drop in the bucket.

– Vince
yesterday





No, you can't read ahead without losing the current row. You need to use cursor as it actually operates, not how you wish it did. What you can do is save your values in a dictionary with a SearchCursor scan with an OID key, the run an update pass later. I've cached tens of millions of rows with a 32-bit Python and hundreds of millions with 64-bit Python, so 15k rows is a drop in the bucket.

– Vince
yesterday










2 Answers
2






active

oldest

votes


















0














Yes, this is expected as it's how python generators work, once a value is consumed, it's gone. If your feature class isn't toooo big, pull all the rows into a list, then use enumerate and indexing to access the next row. E.g.



cursor = ((i,) for i in range(10)) # Fake a cursor as I'm not at a PC with ArcGIS
rows = list(cursor) # Exhaust the generator
for i, row in enumerate(rows):
try:
row_next = rows[i+1]
print(row[0],row_next[0])
except IndexError:
print('no rows')


Output:



0 1
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
no rows


Or... turn it around, grab the first row before the loop then refer to the previous row instead of the next row:



prev_row = next(cursor)
for row in cursor:
print(prev_row[0], row[0])
prev_row = row


Output:



0 1
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9





share|improve this answer























  • I'm doing this for many files, and my smallest one has about 15k rows. As stated in question, purpose is to perform UpdateCursor on a specific column based on values in another column. The printing of the oid values was for illustration of skipping behavior.

    – AlexS1
    yesterday












  • Either method should work. Use a SearchCursor to get the list before using the UpdateCursor, a few million values in a list isn't going to be an issue (unless you pull in geometry as well).

    – user2856
    yesterday











  • answer accepted because it addresses cursor behavior in original question; in case anyone has a similar issue, I posted working code below as another answer

    – AlexS1
    yesterday


















0














Thanks to Vince's comments and user2856's answer/comments, the following worked. Elapsed time = 30.17 seconds for 52,000 row, which seems slow.



temp1 = r'mypathmypointfile.shp'
import datetime
now = datetime.datetime.now()

cursor = arcpy.da.SearchCursor(temp1, ['OID@', 'group', 'id'])
rows = list(cursor)
dict_ids =
for i, row in enumerate(rows):
oid = row[0]
group_current = row[1]
id_current = row[2]
if id_current == 1:
check = "keep"
else:
try:
row_next = rows[i + 1]
id_next = row_next[2]
if vid_next == 1:
check = "keep"
else:
check = "remove"
except IndexError:
check = "keep"
except:
print 'something else went wrong'
dict_ids.update([(oid, check)])
del i, row, check
with arcpy.da.UpdateCursor(temp1, ['OID@']) as cursor:
for row in cursor:
oid = row[0]
if oid in dict_ids:
check = dict_ids.get(oid)
else:
print 'error with dictionary'
sys.exit()
if check != "keep":
cursor.deleteRow()

later = datetime.datetime.now()
elapsed = later - now
print elapsed





share|improve this answer























  • That is slow, your code takes 2 seconds for me on a local FGDB feature class with 52,000 records. Make sure you're not running it on data on a network drive.

    – user2856
    3 hours ago











  • yes, it is slow. no network, all local.

    – AlexS1
    2 hours ago











Your Answer








StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "79"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f316948%2fusing-cursor-next-is-resetting-row-in-cursor-causing-it-to-skip-every-other%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Yes, this is expected as it's how python generators work, once a value is consumed, it's gone. If your feature class isn't toooo big, pull all the rows into a list, then use enumerate and indexing to access the next row. E.g.



cursor = ((i,) for i in range(10)) # Fake a cursor as I'm not at a PC with ArcGIS
rows = list(cursor) # Exhaust the generator
for i, row in enumerate(rows):
try:
row_next = rows[i+1]
print(row[0],row_next[0])
except IndexError:
print('no rows')


Output:



0 1
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
no rows


Or... turn it around, grab the first row before the loop then refer to the previous row instead of the next row:



prev_row = next(cursor)
for row in cursor:
print(prev_row[0], row[0])
prev_row = row


Output:



0 1
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9





share|improve this answer























  • I'm doing this for many files, and my smallest one has about 15k rows. As stated in question, purpose is to perform UpdateCursor on a specific column based on values in another column. The printing of the oid values was for illustration of skipping behavior.

    – AlexS1
    yesterday












  • Either method should work. Use a SearchCursor to get the list before using the UpdateCursor, a few million values in a list isn't going to be an issue (unless you pull in geometry as well).

    – user2856
    yesterday











  • answer accepted because it addresses cursor behavior in original question; in case anyone has a similar issue, I posted working code below as another answer

    – AlexS1
    yesterday















0














Yes, this is expected as it's how python generators work, once a value is consumed, it's gone. If your feature class isn't toooo big, pull all the rows into a list, then use enumerate and indexing to access the next row. E.g.



cursor = ((i,) for i in range(10)) # Fake a cursor as I'm not at a PC with ArcGIS
rows = list(cursor) # Exhaust the generator
for i, row in enumerate(rows):
try:
row_next = rows[i+1]
print(row[0],row_next[0])
except IndexError:
print('no rows')


Output:



0 1
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
no rows


Or... turn it around, grab the first row before the loop then refer to the previous row instead of the next row:



prev_row = next(cursor)
for row in cursor:
print(prev_row[0], row[0])
prev_row = row


Output:



0 1
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9





share|improve this answer























  • I'm doing this for many files, and my smallest one has about 15k rows. As stated in question, purpose is to perform UpdateCursor on a specific column based on values in another column. The printing of the oid values was for illustration of skipping behavior.

    – AlexS1
    yesterday












  • Either method should work. Use a SearchCursor to get the list before using the UpdateCursor, a few million values in a list isn't going to be an issue (unless you pull in geometry as well).

    – user2856
    yesterday











  • answer accepted because it addresses cursor behavior in original question; in case anyone has a similar issue, I posted working code below as another answer

    – AlexS1
    yesterday













0












0








0







Yes, this is expected as it's how python generators work, once a value is consumed, it's gone. If your feature class isn't toooo big, pull all the rows into a list, then use enumerate and indexing to access the next row. E.g.



cursor = ((i,) for i in range(10)) # Fake a cursor as I'm not at a PC with ArcGIS
rows = list(cursor) # Exhaust the generator
for i, row in enumerate(rows):
try:
row_next = rows[i+1]
print(row[0],row_next[0])
except IndexError:
print('no rows')


Output:



0 1
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
no rows


Or... turn it around, grab the first row before the loop then refer to the previous row instead of the next row:



prev_row = next(cursor)
for row in cursor:
print(prev_row[0], row[0])
prev_row = row


Output:



0 1
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9





share|improve this answer













Yes, this is expected as it's how python generators work, once a value is consumed, it's gone. If your feature class isn't toooo big, pull all the rows into a list, then use enumerate and indexing to access the next row. E.g.



cursor = ((i,) for i in range(10)) # Fake a cursor as I'm not at a PC with ArcGIS
rows = list(cursor) # Exhaust the generator
for i, row in enumerate(rows):
try:
row_next = rows[i+1]
print(row[0],row_next[0])
except IndexError:
print('no rows')


Output:



0 1
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
no rows


Or... turn it around, grab the first row before the loop then refer to the previous row instead of the next row:



prev_row = next(cursor)
for row in cursor:
print(prev_row[0], row[0])
prev_row = row


Output:



0 1
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9






share|improve this answer












share|improve this answer



share|improve this answer










answered yesterday









user2856user2856

30.6k258106




30.6k258106












  • I'm doing this for many files, and my smallest one has about 15k rows. As stated in question, purpose is to perform UpdateCursor on a specific column based on values in another column. The printing of the oid values was for illustration of skipping behavior.

    – AlexS1
    yesterday












  • Either method should work. Use a SearchCursor to get the list before using the UpdateCursor, a few million values in a list isn't going to be an issue (unless you pull in geometry as well).

    – user2856
    yesterday











  • answer accepted because it addresses cursor behavior in original question; in case anyone has a similar issue, I posted working code below as another answer

    – AlexS1
    yesterday

















  • I'm doing this for many files, and my smallest one has about 15k rows. As stated in question, purpose is to perform UpdateCursor on a specific column based on values in another column. The printing of the oid values was for illustration of skipping behavior.

    – AlexS1
    yesterday












  • Either method should work. Use a SearchCursor to get the list before using the UpdateCursor, a few million values in a list isn't going to be an issue (unless you pull in geometry as well).

    – user2856
    yesterday











  • answer accepted because it addresses cursor behavior in original question; in case anyone has a similar issue, I posted working code below as another answer

    – AlexS1
    yesterday
















I'm doing this for many files, and my smallest one has about 15k rows. As stated in question, purpose is to perform UpdateCursor on a specific column based on values in another column. The printing of the oid values was for illustration of skipping behavior.

– AlexS1
yesterday






I'm doing this for many files, and my smallest one has about 15k rows. As stated in question, purpose is to perform UpdateCursor on a specific column based on values in another column. The printing of the oid values was for illustration of skipping behavior.

– AlexS1
yesterday














Either method should work. Use a SearchCursor to get the list before using the UpdateCursor, a few million values in a list isn't going to be an issue (unless you pull in geometry as well).

– user2856
yesterday





Either method should work. Use a SearchCursor to get the list before using the UpdateCursor, a few million values in a list isn't going to be an issue (unless you pull in geometry as well).

– user2856
yesterday













answer accepted because it addresses cursor behavior in original question; in case anyone has a similar issue, I posted working code below as another answer

– AlexS1
yesterday





answer accepted because it addresses cursor behavior in original question; in case anyone has a similar issue, I posted working code below as another answer

– AlexS1
yesterday













0














Thanks to Vince's comments and user2856's answer/comments, the following worked. Elapsed time = 30.17 seconds for 52,000 row, which seems slow.



temp1 = r'mypathmypointfile.shp'
import datetime
now = datetime.datetime.now()

cursor = arcpy.da.SearchCursor(temp1, ['OID@', 'group', 'id'])
rows = list(cursor)
dict_ids =
for i, row in enumerate(rows):
oid = row[0]
group_current = row[1]
id_current = row[2]
if id_current == 1:
check = "keep"
else:
try:
row_next = rows[i + 1]
id_next = row_next[2]
if vid_next == 1:
check = "keep"
else:
check = "remove"
except IndexError:
check = "keep"
except:
print 'something else went wrong'
dict_ids.update([(oid, check)])
del i, row, check
with arcpy.da.UpdateCursor(temp1, ['OID@']) as cursor:
for row in cursor:
oid = row[0]
if oid in dict_ids:
check = dict_ids.get(oid)
else:
print 'error with dictionary'
sys.exit()
if check != "keep":
cursor.deleteRow()

later = datetime.datetime.now()
elapsed = later - now
print elapsed





share|improve this answer























  • That is slow, your code takes 2 seconds for me on a local FGDB feature class with 52,000 records. Make sure you're not running it on data on a network drive.

    – user2856
    3 hours ago











  • yes, it is slow. no network, all local.

    – AlexS1
    2 hours ago















0














Thanks to Vince's comments and user2856's answer/comments, the following worked. Elapsed time = 30.17 seconds for 52,000 row, which seems slow.



temp1 = r'mypathmypointfile.shp'
import datetime
now = datetime.datetime.now()

cursor = arcpy.da.SearchCursor(temp1, ['OID@', 'group', 'id'])
rows = list(cursor)
dict_ids =
for i, row in enumerate(rows):
oid = row[0]
group_current = row[1]
id_current = row[2]
if id_current == 1:
check = "keep"
else:
try:
row_next = rows[i + 1]
id_next = row_next[2]
if vid_next == 1:
check = "keep"
else:
check = "remove"
except IndexError:
check = "keep"
except:
print 'something else went wrong'
dict_ids.update([(oid, check)])
del i, row, check
with arcpy.da.UpdateCursor(temp1, ['OID@']) as cursor:
for row in cursor:
oid = row[0]
if oid in dict_ids:
check = dict_ids.get(oid)
else:
print 'error with dictionary'
sys.exit()
if check != "keep":
cursor.deleteRow()

later = datetime.datetime.now()
elapsed = later - now
print elapsed





share|improve this answer























  • That is slow, your code takes 2 seconds for me on a local FGDB feature class with 52,000 records. Make sure you're not running it on data on a network drive.

    – user2856
    3 hours ago











  • yes, it is slow. no network, all local.

    – AlexS1
    2 hours ago













0












0








0







Thanks to Vince's comments and user2856's answer/comments, the following worked. Elapsed time = 30.17 seconds for 52,000 row, which seems slow.



temp1 = r'mypathmypointfile.shp'
import datetime
now = datetime.datetime.now()

cursor = arcpy.da.SearchCursor(temp1, ['OID@', 'group', 'id'])
rows = list(cursor)
dict_ids =
for i, row in enumerate(rows):
oid = row[0]
group_current = row[1]
id_current = row[2]
if id_current == 1:
check = "keep"
else:
try:
row_next = rows[i + 1]
id_next = row_next[2]
if vid_next == 1:
check = "keep"
else:
check = "remove"
except IndexError:
check = "keep"
except:
print 'something else went wrong'
dict_ids.update([(oid, check)])
del i, row, check
with arcpy.da.UpdateCursor(temp1, ['OID@']) as cursor:
for row in cursor:
oid = row[0]
if oid in dict_ids:
check = dict_ids.get(oid)
else:
print 'error with dictionary'
sys.exit()
if check != "keep":
cursor.deleteRow()

later = datetime.datetime.now()
elapsed = later - now
print elapsed





share|improve this answer













Thanks to Vince's comments and user2856's answer/comments, the following worked. Elapsed time = 30.17 seconds for 52,000 row, which seems slow.



temp1 = r'mypathmypointfile.shp'
import datetime
now = datetime.datetime.now()

cursor = arcpy.da.SearchCursor(temp1, ['OID@', 'group', 'id'])
rows = list(cursor)
dict_ids =
for i, row in enumerate(rows):
oid = row[0]
group_current = row[1]
id_current = row[2]
if id_current == 1:
check = "keep"
else:
try:
row_next = rows[i + 1]
id_next = row_next[2]
if vid_next == 1:
check = "keep"
else:
check = "remove"
except IndexError:
check = "keep"
except:
print 'something else went wrong'
dict_ids.update([(oid, check)])
del i, row, check
with arcpy.da.UpdateCursor(temp1, ['OID@']) as cursor:
for row in cursor:
oid = row[0]
if oid in dict_ids:
check = dict_ids.get(oid)
else:
print 'error with dictionary'
sys.exit()
if check != "keep":
cursor.deleteRow()

later = datetime.datetime.now()
elapsed = later - now
print elapsed






share|improve this answer












share|improve this answer



share|improve this answer










answered yesterday









AlexS1AlexS1

16710




16710












  • That is slow, your code takes 2 seconds for me on a local FGDB feature class with 52,000 records. Make sure you're not running it on data on a network drive.

    – user2856
    3 hours ago











  • yes, it is slow. no network, all local.

    – AlexS1
    2 hours ago

















  • That is slow, your code takes 2 seconds for me on a local FGDB feature class with 52,000 records. Make sure you're not running it on data on a network drive.

    – user2856
    3 hours ago











  • yes, it is slow. no network, all local.

    – AlexS1
    2 hours ago
















That is slow, your code takes 2 seconds for me on a local FGDB feature class with 52,000 records. Make sure you're not running it on data on a network drive.

– user2856
3 hours ago





That is slow, your code takes 2 seconds for me on a local FGDB feature class with 52,000 records. Make sure you're not running it on data on a network drive.

– user2856
3 hours ago













yes, it is slow. no network, all local.

– AlexS1
2 hours ago





yes, it is slow. no network, all local.

– AlexS1
2 hours ago

















draft saved

draft discarded
















































Thanks for contributing an answer to Geographic Information Systems Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f316948%2fusing-cursor-next-is-resetting-row-in-cursor-causing-it-to-skip-every-other%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

រឿង រ៉ូមេអូ និង ហ្ស៊ុយលីយេ សង្ខេបរឿង តួអង្គ បញ្ជីណែនាំ

Crop image to path created in TikZ? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)Crop an inserted image?TikZ pictures does not appear in posterImage behind and beyond crop marks?Tikz picture as large as possible on A4 PageTransparency vs image compression dilemmaHow to crop background from image automatically?Image does not cropTikzexternal capturing crop marks when externalizing pgfplots?How to include image path that contains a dollar signCrop image with left size given

Romeo and Juliet ContentsCharactersSynopsisSourcesDate and textThemes and motifsCriticism and interpretationLegacyScene by sceneSee alsoNotes and referencesSourcesExternal linksNavigation menu"Consumer Price Index (estimate) 1800–"10.2307/28710160037-3222287101610.1093/res/II.5.31910.2307/45967845967810.2307/2869925286992510.1525/jams.1982.35.3.03a00050"Dada Masilo: South African dancer who breaks the rules"10.1093/res/os-XV.57.1610.2307/28680942868094"Sweet Sorrow: Mann-Korman's Romeo and Juliet Closes Sept. 5 at MN's Ordway"the original10.2307/45957745957710.1017/CCOL0521570476.009"Ram Leela box office collections hit massive Rs 100 crore, pulverises prediction"Archived"Broadway Revival of Romeo and Juliet, Starring Orlando Bloom and Condola Rashad, Will Close Dec. 8"Archived10.1075/jhp.7.1.04hon"Wherefore art thou, Romeo? To make us laugh at Navy Pier"the original10.1093/gmo/9781561592630.article.O006772"Ram-leela Review Roundup: Critics Hail Film as Best Adaptation of Romeo and Juliet"Archived10.2307/31946310047-77293194631"Romeo and Juliet get Twitter treatment""Juliet's Nurse by Lois Leveen""Romeo and Juliet: Orlando Bloom's Broadway Debut Released in Theaters for Valentine's Day"Archived"Romeo and Juliet Has No Balcony"10.1093/gmo/9781561592630.article.O00778110.2307/2867423286742310.1076/enst.82.2.115.959510.1080/00138380601042675"A plague o' both your houses: error in GCSE exam paper forces apology""Juliet of the Five O'Clock Shadow, and Other Wonders"10.2307/33912430027-4321339124310.2307/28487440038-7134284874410.2307/29123140149-661129123144728341M"Weekender Guide: Shakespeare on The Drive""balcony"UK public library membership"romeo"UK public library membership10.1017/CCOL9780521844291"Post-Zionist Critique on Israel and the Palestinians Part III: Popular Culture"10.2307/25379071533-86140377-919X2537907"Capulets and Montagues: UK exam board admit mixing names up in Romeo and Juliet paper"Istoria Novellamente Ritrovata di Due Nobili Amanti2027/mdp.390150822329610820-750X"GCSE exam error: Board accidentally rewrites Shakespeare"10.2307/29176390149-66112917639"Exam board apologises after error in English GCSE paper which confused characters in Shakespeare's Romeo and Juliet""From Mariotto and Ganozza to Romeo and Guilietta: Metamorphoses of a Renaissance Tale"10.2307/37323537323510.2307/2867455286745510.2307/28678912867891"10 Questions for Taylor Swift"10.2307/28680922868092"Haymarket Theatre""The Zeffirelli Way: Revealing Talk by Florentine Director""Michael Smuin: 1938-2007 / Prolific dance director had showy career"The Life and Art of Edwin BoothRomeo and JulietRomeo and JulietRomeo and JulietRomeo and JulietEasy Read Romeo and JulietRomeo and Julieteeecb12003684p(data)4099369-3n8211610759dbe00d-a9e2-41a3-b2c1-977dd692899302814385X313670221313670221