1

I am trying to identify duplicate records between two feature classes in a file gdb (Arc 10.3). I first create a set of unique IDs present in FC_1, then use an if statement to identify whether the IDs in FC_2 are present in FC_1.

checkfield = "gisID"
unique_values = set(row[0] for row in arcpy.da.SearchCursor(FC_1, checkfield))

with arcpy.da.UpdateCursor (FC_2, [checkfield, "Type"]) as uCurs:
        for uRow in uCurs:
            print (uRow[0])
            if uRow [0] in unique_values:
                uRow [1] = "duplicate"
                uCurs.updateRow(uRow)
            else:
                uRow [1] = "unique"
                uCurs.updateRow(uRow)

    # check if uRow[0] is in the set
    if 200683 in unique_values:
        print ("200683 is in the set")

I've purposefully set up the feature classes such that the 2 records in FC_2 have IDs already present in FC_1. The last 3 lines in the script are there to check this.

However, FC_2 "Type" field only updates with "unique", when these are clearly duplicates. Any advice on how to correct this?

  • 1
    Have a read of stackoverflow.com/questions/8705378/pythons-in-set-operator, I think a set here mightn't be a good idea. Perhaps make unique_values a list like unique_values = [row[0] for row in arcpy.da.SearchCursor(FC_1, checkfield)]. What is the variable type for gisID? if it's a string then comparison would be best done in upper or lower case. – Michael Stimson Jun 21 '17 at 21:32
  • 1
    @MichaelMiles-Stimson, thank you for the insight. That was indeed the error. I was comparing one field that was string, vs another that was an integer. I converted the string field to a numeric field, made some adjustments to the script, and the script ran well. – saoirse Jun 21 '17 at 23:51
  • Could you please post your working code as an answer to your own question for future users with the same problem. – Michael Stimson Jun 22 '17 at 0:15
2

Here is the code that I used to assign rows as either duplicate or unique. Prior to this, I converted the two fields that are compared to be of the same type (integer, rather than string). My error resulted from comparing a string field with a long integer field.

 checkfield = "gisID"
    ID_values = []
    with arcpy.da.SearchCursor(FC_1, [checkfield]) as cursor:
            for row in cursor:
                ID_values.append(row[0])
    del cursor

    with arcpy.da.UpdateCursor (FC_2, [checkfield, "Type"]) as uCurs:
        for uRow in uCurs:
            print (uRow[0])
            if uRow [0] in ID_values:
                uRow [1] = "duplicate"
                uCurs.updateRow(uRow)
            else:
                uRow [1] = "unique"
                uCurs.updateRow(uRow)

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.