ArcPy – Creating a Layer from Python Dictionary Using ArcPy

arcmaparcpydictionary

I'm writing a script to find duplicate cables in a layer. I want to create a layer from the result to find the closest cables to those already cut with ArcPy but I can't find a way to do it.

The license I use is Standard so I don't have the FindIdentical_management function.

here is my code :

couche_cable = arcpy.MakeFeatureLayer_management(chem_caftth)
champ_a_verfier = ["REFERENCE", "OBJECTID", "CREATIONUSER", "LASTUSER", "USERREFERENCE", "SUBTYPEID", "SHAPE"]  
    values={}
    values["Ref_Nulle"]=0
    with arcpy.da.SearchCursor(couche_cable, champ_a_verfier) as rows:
        for r in rows:
            if r[0] in [None,""]:
                values["Ref_Nulle"]+=1
            else:
                if r[0] not in values:
                    values[r[0]]=1
                else:
                    values[r[0]]+=1 
    del rows
    
    
    dictDoublons = {}
    dictPasDoublons = {}
    cable_cda = 'CDA'
    count = 0
    for item in values:
        if values[item] > 1 and values[item] < 3:
            if not item.startswith("CDA"):
                dictDoublons[item] = 'Doublons'
                count+=1
                arcpy.AddWarning('Il y a {} cables en doublons  a Verifier / Corriger.'.format(count))
        else:   
            dictPasDoublons[item] = 'PasDoublons'

Best Answer

You can use a set:

import arcpy

fc = r'C:\GIS\ArcMap_default_folder\Default.gdb\my_sample'
fieldlist = ['kkod','kategori'] #Fields to consider for duplicates

values = set() #Create a set, which cant have duplicate values
oids = [] #A list to store object ids
for row in arcpy.da.SearchCursor(fc, ['OID@']+fieldlist):
    if row[1:] in values: #If the row values are in the set = they are a duplicate
        oids.append(row[0]) #Add the oid to oids list
    else:
        values.add(row[1:]) #If not add the values to the set

#Select the oids in oids list
oidfield = arcpy.Describe(fc).OIDFieldName
sql = """{0} IN{1}""".format(arcpy.AddFieldDelimiters(datasource=fc, field=oidfield), tuple(oids))
arcpy.SelectLayerByAttribute_management(in_layer_or_view='my_sample', where_clause=sql) #Or Select, MakeFeatureLayer etc.

enter image description here

Related Question