Wednesday, 15 February 2012

Arcpy Code: Find duplicate values in an ArcGIS feature class field

I'm finding coding in arcpy a little more frustrating than I would like and decided to share useful code here in case it helps anyone else trying to the same or similar things.


###########################################
# identifyduplicates.py
# Created by: Mairead de Roiste, Victoria University of Wellington
# Date: 16 Feb 2012
# What does it do?: prints to screen a list of the values that are

# duplicates in a particular field in an ArcGIS feature class
###########################################


# Import arcpy module
import arcpy

# set up variables
# the feature class you want to sort then search

fc = "D:\\ISCR_transport\\code\\data\\NZTA_workhomepoints\\workhomepoints.gdb\\workloc"

# field to sort in ascending order
fieldname = "FIRST_uniquePerson"fieldascend = fieldname + " A"

# create UpdateCursor to search through the rows, field above sorted in ascending order
rows = arcpy.SearchCursor(fc,"","","", fieldascend)

#Create an empty list
dupValuesList = []

# the code needs to be updated for the name of the field
i = -1
for row in rows:
    if i == -1: #first time around       

        value = row.FIRST_uniquePerson
        i += 1
        print value, i
    elif row.FIRST_uniquePerson != value: #if a new ID       

        value = row.FIRST_uniquePerson
        i = 0
    else:
        dupValuesList.append(value)

print dupValuesList

1 comment:

  1. Hi, I used your script to base mine, here is an improvement for the loop:

    row = rows.next()
    value = row.getValue(fieldname)
    for row in rows:
    if row.getValue(fieldname) != value:
    value = row.getValue(fieldname)
    else:
    dupValuesList.append(value)

    ReplyDelete