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?
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
add a comment |
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
2
next
isn't needed, because you're in afor
loop. You can't read ahead, but you can preserve the current inprevious
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
add a comment |
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
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
arcpy cursor arcgis-10.6
edited yesterday
AlexS1
asked yesterday
AlexS1AlexS1
16710
16710
2
next
isn't needed, because you're in afor
loop. You can't read ahead, but you can preserve the current inprevious
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
add a comment |
2
next
isn't needed, because you're in afor
loop. You can't read ahead, but you can preserve the current inprevious
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
add a comment |
2 Answers
2
active
oldest
votes
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
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 aSearchCursor
to get the list before using theUpdateCursor
, 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
add a comment |
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
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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
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 aSearchCursor
to get the list before using theUpdateCursor
, 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
add a comment |
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
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 aSearchCursor
to get the list before using theUpdateCursor
, 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
add a comment |
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
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
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 aSearchCursor
to get the list before using theUpdateCursor
, 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
add a comment |
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 aSearchCursor
to get the list before using theUpdateCursor
, 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
add a comment |
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
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
add a comment |
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
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
add a comment |
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
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
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
2
next
isn't needed, because you're in afor
loop. You can't read ahead, but you can preserve the current inprevious
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