I have accomplished this task in the field calculator (thanks to another post here, but I now realize that I need to do it using arcpy).

I have code that perfectly marks the second, third etc. duplicate(s) but I need it to mark all values that have a duplicate. Basically, I can't figure out how to mark the FIRST instance as well as the rest. Here's the code:

import arcpy

inFeatures = "thephaseIIIlayer"
fieldName = "PhaseIII_ID"

desc = arcpy.Describe(inFeatures)
fields = desc.fields
rows = arcpy.UpdateCursor("thephaseIIIlayer", "", "", "PhaseIII_ID", "PhaseIII_ID A") 

    i = 0
    for row in rows:
        if i == 0: 
        value = row.PhaseIII_ID
    i += 1
    if i > 1
        row.SplitMarker = i
    if row.PhaseIII_ID != value: 
        value = row.PhaseIII_ID
        i = 1
    row.SplitMarker = i -1

del row 
del rows
  • 2
    I think this is easier to do in an ArcPy script using Summary Statistics (to get a FREQUENCY field) and then Join Fields - are you open to that as a solution or wedded to cursors? What version of ArcGIS for Desktop are you using?
    – PolyGeo
    Oct 8 '13 at 22:41
  • I am using 10.1 but could easily upgrade to 10.2 if I got around to it. I wanted to avoid creating secondary tables but I think I can see where you are going there.
    – indybrown
    Oct 8 '13 at 23:39
  • 2
    You could cursor through the table first using a search cursor, add the values that have duplicates into a list and then follow that with an update cursor: if list.find(value) > 0. list.find is case sensitive if that is important. Oct 9 '13 at 0:06
  • The secondary table would only be temporary because you can delete it after joining its FREQUENCY field. @MichaelMiles-Stimson technique (+1) would work fine too but if you have lots of rows use the arcpy.da cursors.
    – PolyGeo
    Oct 9 '13 at 0:12
  • So no real answers I guess because no one wants to write the code. @indybrown find a solution and post as an answer to answer your own question. gis.stackexchange.com/help/self-answer
    – Brad Nesom
    Oct 9 '13 at 1:40

Here it is, the simplest thing ever and as an added bonus it updates an external table with the information and a replacement value. My only worry is that it only works if there is only one duplicated value and returns the last one. Since my users will likely only be splitting polygons one at a time, it shouldn't be a big deal but it is a limitation for sure.

import arcpy
from arcpy import env

outtable = "C:\SCRATCH\scratch.gdb\MEfreq2"
histtable = "C:\SCRATCH\scratch.gdb\History"
expression = arcgis.rand('Integer 0 10000000')
expression2 = arcgis.rand('Integer 0 10')
randomness= int(expression + expression2)

arcpy.Frequency_analysis(dataset, outtable,["PhaseIII_ID"])

rows = arcpy.da.SearchCursor(outtable, ['FREQUENCY', 'PhaseIII_ID'])

hamsterpants = 0
for row in rows:
    if row[0] >1:
        #print row[1]
        hamsterpants = row[1]
print hamsterpants

cursor = arcpy.da.InsertCursor (histtable, ("PhaseIII_Parent", "PhaseIII_ID"))
cursor.insertRow((hamsterpants, randomness))

del row
del cursor

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.