Python Sequential Number Gen starting value

1403
2
04-11-2011 11:01 AM
GabeMarcello
New Contributor
Looking for a way to adapt this Python sequential number generator to use the LAST value +1 in a given field for pStart:

rec=0
def autoIncrement():
global rec
pStart = 1 #adjust start value, if req'd
pInterval = 1 #adjust interval value, if req'd
if (rec == 0):
  rec = pStart
else:
  rec = rec + pInterval
return rec

autoIncrement()
Tags (2)
0 Kudos
2 Replies
DarrenWiens2
MVP Honored Contributor
There's probably a one-liner function out there that tells you the last number in a list of numbers, but this works:
import arcpy

input = "H:/GIS_Data/TEMP_GDB.gdb/feattoline" # your feature class
field = "OBJECTID A" # the name of the field, sorted ascending

rows = arcpy.SearchCursor(input,"","","",field)

for row in rows:
    number = row.OBJECTID

This code simply sorts the values in the field "OBJECTID," assigns and overwrites that value to the variable "number" for each increasing value. Finally, you're left with the largest value of "OBJECTID." Then, make pStart = number.
0 Kudos
GabeMarcello
New Contributor
There's probably a one-liner function out there that tells you the last number in a list of numbers, but this works:
import arcpy

input = "H:/GIS_Data/TEMP_GDB.gdb/feattoline" # your feature class
field = "OBJECTID A" # the name of the field, sorted ascending

rows = arcpy.SearchCursor(input,"","","",field)

for row in rows:
    number = row.OBJECTID

This code simply sorts the values in the field "OBJECTID," assigns and overwrites that value to the variable "number" for each increasing value. Finally, you're left with the largest value of "OBJECTID." Then, make pStart = number.


This worked exactly how I wanted it to. Thank you. You truly are the wind beneath my wings.
0 Kudos