Find duplicates and give incrementing value

814
3
Jump to solution
10-08-2013 08:09 AM
RodC
by
New Contributor
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]
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
DouglasSands
Occasional Contributor II
Have you tried using a dictionary? Perhaps something along these lines:

def main():      workspace = arcpy.GetParameterAsText(0)     arcpy.env.workspace = workspace     field = "newId"       newidfield = arcpy.AddField_management(workspace,'newId','TEXT','','','','','NULLABLE')      id_tracking = {}      cursor = arcpy.UpdateCursor(workspace)     for row in cursor:         current_id = row.getValue(field)         if current_id in id_tracking:             count = id_tracking[current_id]             count += 1             row.setValue(field,str(count).zfill(2))             id_tracking[current_id] = count         else:             count = 1             row.setValue(field,str(count).zfill(2))             id_tracking[current_id] = count         cursor.updateRow(row)     new_field = arcpy.CalculateField_management(workspace,"newId","!oldId!"+"!newId!",'PYTHON')


As an added bonus, you don't need the autoIncrement function anymore.

View solution in original post

0 Kudos
3 Replies
DouglasSands
Occasional Contributor II
Have you tried using a dictionary? Perhaps something along these lines:

def main():      workspace = arcpy.GetParameterAsText(0)     arcpy.env.workspace = workspace     field = "newId"       newidfield = arcpy.AddField_management(workspace,'newId','TEXT','','','','','NULLABLE')      id_tracking = {}      cursor = arcpy.UpdateCursor(workspace)     for row in cursor:         current_id = row.getValue(field)         if current_id in id_tracking:             count = id_tracking[current_id]             count += 1             row.setValue(field,str(count).zfill(2))             id_tracking[current_id] = count         else:             count = 1             row.setValue(field,str(count).zfill(2))             id_tracking[current_id] = count         cursor.updateRow(row)     new_field = arcpy.CalculateField_management(workspace,"newId","!oldId!"+"!newId!",'PYTHON')


As an added bonus, you don't need the autoIncrement function anymore.
0 Kudos
RodC
by
New Contributor
Have you tried using a dictionary? Perhaps something along these lines:

def main():

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


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

    id_tracking = {}

    cursor = arcpy.UpdateCursor(workspace)
    for row in cursor:
        current_id = row.getValue(field)
        if current_id in id_tracking:
            count = id_tracking[current_id]
            count += 1
            row.setValue(field,str(count).zfill(2))
            id_tracking[current_id] = count
        else:
            count = 1
            row.setValue(field,str(count).zfill(2))
            id_tracking[current_id] = count
        cursor.updateRow(row)
    new_field = arcpy.CalculateField_management(workspace,"newId","!oldId!"+"!newId!",'PYTHON')


As an added bonus, you don't need the autoIncrement function anymore.



Awesome this worked!, just added oldid_field = "oldId" and changed current_id = row.getValue(field) to => current_id = row.getValue(oldId_field)

to get those duplicates to increment.

Thanks
0 Kudos
RobertStokes
New Contributor III
Hey Rodrigo, where in the code did you place the oldId_field = "oldId"? Can you post your complete code?

Sincerely,

Robert
0 Kudos