Skip to main content
added 2 characters in body
Source Link
Michael Stimson
  • 25.7k
  • 2
  • 35
  • 75
# get a list of all the values of fieldA
# this ONLY works for shapefiles
NextValues = [row[0] for row in arcpy.da.SeachCursorSearchCursor(copyFile,'FieldA')] 

with arcpy.da.UpdateCursor(copyFile, ['OID@', 'FieldA', 'FieldB']) as cursor:
    counter = 1
    for row in cursor:
        NextFID = row[0] + 1 # the index of the next value
        row[2] = counter
        fieldA = row[1]
        # get next fieldA
        # make sure this isn't the last record or there will be no next
        if NextFID < len(NextValues):
            if fieldA == NextValues[NextFID]:
                counter += 1
            else:
                counter = 1
            cursor.updateRow(row)
        else:
            # do something here for the last record
# get a list of all the values of fieldA
NextValues = [row[0] for row in arcpy.da.SeachCursorSearchCursor(copyFile,'FieldA')] 

with arcpy.da.UpdateCursor(copyFile, ['OID@', 'FieldA', 'FieldB']) as cursor:
    counter = 1
    NextRow = 1
    for row in cursor:
        row[2] = counter
        fieldA = row[1]
        # get next fieldA
        # make sure this isn't the last record or there will be no next
        if NextRow < len(NextValues):
            if fieldA == NextValues[NextRow]:
                counter += 1
            else:
                counter = 1
            cursor.updateRow(row)
        else:
            # do something here for the last record
        NextRow += 1
# get a list of all the values of fieldA
# this ONLY works for shapefiles
NextValues = [row[0] for row in arcpy.da.SeachCursor(copyFile,'FieldA')] 

with arcpy.da.UpdateCursor(copyFile, ['OID@', 'FieldA', 'FieldB']) as cursor:
    counter = 1
    for row in cursor:
        NextFID = row[0] + 1 # the index of the next value
        row[2] = counter
        fieldA = row[1]
        # get next fieldA
        # make sure this isn't the last record or there will be no next
        if NextFID < len(NextValues):
            if fieldA == NextValues[NextFID]:
                counter += 1
            else:
                counter = 1
            cursor.updateRow(row)
        else:
            # do something here for the last record
# get a list of all the values of fieldA
NextValues = [row[0] for row in arcpy.da.SeachCursor(copyFile,'FieldA')] 

with arcpy.da.UpdateCursor(copyFile, ['OID@', 'FieldA', 'FieldB']) as cursor:
    counter = 1
    NextRow = 1
    for row in cursor:
        row[2] = counter
        fieldA = row[1]
        # get next fieldA
        # make sure this isn't the last record or there will be no next
        if NextRow < len(NextValues):
            if fieldA == NextValues[NextRow]:
                counter += 1
            else:
                counter = 1
            cursor.updateRow(row)
        else:
            # do something here for the last record
        NextRow += 1
# get a list of all the values of fieldA
# this ONLY works for shapefiles
NextValues = [row[0] for row in arcpy.da.SearchCursor(copyFile,'FieldA')] 

with arcpy.da.UpdateCursor(copyFile, ['OID@', 'FieldA', 'FieldB']) as cursor:
    counter = 1
    for row in cursor:
        NextFID = row[0] + 1 # the index of the next value
        row[2] = counter
        fieldA = row[1]
        # get next fieldA
        # make sure this isn't the last record or there will be no next
        if NextFID < len(NextValues):
            if fieldA == NextValues[NextFID]:
                counter += 1
            else:
                counter = 1
            cursor.updateRow(row)
        else:
            # do something here for the last record
# get a list of all the values of fieldA
NextValues = [row[0] for row in arcpy.da.SearchCursor(copyFile,'FieldA')] 

with arcpy.da.UpdateCursor(copyFile, ['OID@', 'FieldA', 'FieldB']) as cursor:
    counter = 1
    NextRow = 1
    for row in cursor:
        row[2] = counter
        fieldA = row[1]
        # get next fieldA
        # make sure this isn't the last record or there will be no next
        if NextRow < len(NextValues):
            if fieldA == NextValues[NextRow]:
                counter += 1
            else:
                counter = 1
            cursor.updateRow(row)
        else:
            # do something here for the last record
        NextRow += 1
Source Link
Michael Stimson
  • 25.7k
  • 2
  • 35
  • 75

Give this a try:

# get a list of all the values of fieldA
# this ONLY works for shapefiles
NextValues = [row[0] for row in arcpy.da.SeachCursor(copyFile,'FieldA')] 

with arcpy.da.UpdateCursor(copyFile, ['OID@', 'FieldA', 'FieldB']) as cursor:
    counter = 1
    for row in cursor:
        NextFID = row[0] + 1 # the index of the next value
        row[2] = counter
        fieldA = row[1]
        # get next fieldA
        # make sure this isn't the last record or there will be no next
        if NextFID < len(NextValues):
            if fieldA == NextValues[NextFID]:
                counter += 1
            else:
                counter = 1
            cursor.updateRow(row)
        else:
            # do something here for the last record

If you're using a geodatabase feature class you will need to keep a track of the index as you can't use the OID - it's not guaranteed to be 0 based, unique and contiguous - only 0 based and unique (not contiguous) so calling the next OID value may not retrieve a record:

# get a list of all the values of fieldA
NextValues = [row[0] for row in arcpy.da.SeachCursor(copyFile,'FieldA')] 

with arcpy.da.UpdateCursor(copyFile, ['OID@', 'FieldA', 'FieldB']) as cursor:
    counter = 1
    NextRow = 1
    for row in cursor:
        row[2] = counter
        fieldA = row[1]
        # get next fieldA
        # make sure this isn't the last record or there will be no next
        if NextRow < len(NextValues):
            if fieldA == NextValues[NextRow]:
                counter += 1
            else:
                counter = 1
            cursor.updateRow(row)
        else:
            # do something here for the last record
        NextRow += 1