AnsweredAssumed Answered

Find duplicates and give incrementing value

Question asked by rod.cos on Oct 8, 2013
Latest reply on May 23, 2014 by 13531
I'm trying to tweak this script to find duplicates in a field and give it a sequential value to it.
As of now the script just auto increments by concatenating the "oldId" field and the "newId" field into the "newId' field.


current output:

123 => 12301
123 => 12302
123 => 12303
234 => 23404
237 => 23705

I would want the desired output to be:

123 => 12301
123 => 12302
123 => 12303
234 => 23401
237 => 23701

so, different id's all get 01 at the end and duplicates get sequential values starting with 01 (using .zfill(2)) to keep the leading zero.
the two fields are also TEXT fields.

any suggestions would be great,

Thanks!

[HTML]
import arcpy
import os


def main():

    workspace = arcpy.GetParameterAsText(0)
    arcpy.env.workspace = workspace
    field = "newId"

    newidfield = arcpy.AddField_management(workspace,'newId','TEXT','','','','','NULLABLE')

    cursor = arcpy.UpdateCursor(workspace)
    for row in cursor:
        row.setValue(field,str(autoIncrement()).zfill(2))
        cursor.updateRow(row)
    new_field = arcpy.CalculateField_management(workspace,"newId","!oldId!"+"!newId!",'PYTHON')

rec = 0
def autoIncrement():
    global rec
    pStart = 1
    pInterval = 1
    if (rec == 0):
        rec = pStart
        return rec
    else:
        rec = rec + pInterval
        return rec

if __name__ == '__main__':
    print "Adding IDs..."
    main()
[/HTML]

Outcomes